<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>算法必会篇 | 青灯有味</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="manifest" href="/manifest.json">
    <link rel="icon" href="/favicon.png">
    <link rel="apple-touch-icon" href="/icon_vuepress_reco.png">
    <link rel="mask-icon" href="/icon_vuepress_reco.svg" color="#42b983">
    <script language="javascript" type="text/javascript" src="https://cdn.staticfile.org/jquery/1.7.2/jquery.min.js" rel="defer"></script>
    <script language="javascript" type="text/javascript" src="/js/MouseClickEffect.js" rel="defer"></script>
    <meta name="description" content="一款简洁易懂算法博客 & 文档抠细节 & 主题">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="force-rendering" content="webkit">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <meta name="theme-color" content="#42b983">
    <meta name="keywords" content="前端算法综合查询，办公软件，threejs3D技术开发，vue和react源码讲解，有技术的文档博客">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#000000">
    <meta name="baidu-site-verification" content="aO3jmjhVNd">
    <meta name="robots" content="all">
    <meta name="csdn-baidu-search" content="[object Object]">
    <link rel="preload" href="/assets/css/0.styles.1b43475e.css" as="style"><link rel="preload" href="/assets/js/app.3af45ec9.js" as="script"><link rel="preload" href="/assets/js/6.5ffa3a3c.js" as="script"><link rel="preload" href="/assets/js/1.60e80d20.js" as="script"><link rel="preload" href="/assets/js/7.cb2bf59a.js" as="script"><link rel="preload" href="/assets/js/26.ba28b685.js" as="script"><link rel="preload" href="/assets/js/28.9dabefed.js" as="script"><link rel="preload" href="/assets/js/36.10d521da.js" as="script"><link rel="prefetch" href="/assets/js/10.b6c238a7.js"><link rel="prefetch" href="/assets/js/100.6fba9f18.js"><link rel="prefetch" href="/assets/js/101.79004a5d.js"><link rel="prefetch" href="/assets/js/11.a0d3dca2.js"><link rel="prefetch" href="/assets/js/12.9620ec1e.js"><link rel="prefetch" href="/assets/js/13.8a4c99c1.js"><link rel="prefetch" href="/assets/js/14.e828e497.js"><link rel="prefetch" href="/assets/js/15.85265c81.js"><link rel="prefetch" href="/assets/js/16.5b98bba6.js"><link rel="prefetch" href="/assets/js/17.5938af1b.js"><link rel="prefetch" href="/assets/js/18.24afa132.js"><link rel="prefetch" href="/assets/js/19.f978cfa2.js"><link rel="prefetch" href="/assets/js/2.3d790711.js"><link rel="prefetch" href="/assets/js/20.41e409f0.js"><link rel="prefetch" href="/assets/js/21.038c02f6.js"><link rel="prefetch" href="/assets/js/22.4595e0d7.js"><link rel="prefetch" href="/assets/js/23.6fabf12d.js"><link rel="prefetch" href="/assets/js/24.ddfa7ac5.js"><link rel="prefetch" href="/assets/js/25.fe3e21e0.js"><link rel="prefetch" href="/assets/js/27.29cb30b4.js"><link rel="prefetch" href="/assets/js/29.b110692c.js"><link rel="prefetch" href="/assets/js/3.b52271f3.js"><link rel="prefetch" href="/assets/js/30.db42d4cb.js"><link rel="prefetch" href="/assets/js/31.fe0769c9.js"><link rel="prefetch" href="/assets/js/32.fe344b91.js"><link rel="prefetch" href="/assets/js/33.09556c3f.js"><link rel="prefetch" href="/assets/js/34.0feada29.js"><link rel="prefetch" href="/assets/js/35.80a05aaf.js"><link rel="prefetch" href="/assets/js/37.bd4e8671.js"><link rel="prefetch" href="/assets/js/38.27cec8e2.js"><link rel="prefetch" href="/assets/js/39.793101dd.js"><link rel="prefetch" href="/assets/js/40.0bb89ca4.js"><link rel="prefetch" href="/assets/js/41.43ba7256.js"><link rel="prefetch" href="/assets/js/42.b8b3fad3.js"><link rel="prefetch" href="/assets/js/43.b4ea7ff2.js"><link rel="prefetch" href="/assets/js/44.5d689375.js"><link rel="prefetch" href="/assets/js/45.8985fca2.js"><link rel="prefetch" href="/assets/js/46.c9058cc0.js"><link rel="prefetch" href="/assets/js/47.0e43c612.js"><link rel="prefetch" href="/assets/js/48.7fe8e1b0.js"><link rel="prefetch" href="/assets/js/49.e8a50151.js"><link rel="prefetch" href="/assets/js/50.d0966268.js"><link rel="prefetch" href="/assets/js/51.075b2486.js"><link rel="prefetch" href="/assets/js/52.104c025b.js"><link rel="prefetch" href="/assets/js/53.3e1d2136.js"><link rel="prefetch" href="/assets/js/54.75b0ddc5.js"><link rel="prefetch" href="/assets/js/55.3a80c259.js"><link rel="prefetch" href="/assets/js/56.491226d3.js"><link rel="prefetch" href="/assets/js/57.69c4f560.js"><link rel="prefetch" href="/assets/js/58.dd690d01.js"><link rel="prefetch" href="/assets/js/59.823f14a2.js"><link rel="prefetch" href="/assets/js/60.ab51afcb.js"><link rel="prefetch" href="/assets/js/61.1b6394d6.js"><link rel="prefetch" href="/assets/js/62.d0bcc3c9.js"><link rel="prefetch" href="/assets/js/63.c6f180d6.js"><link rel="prefetch" href="/assets/js/64.150fa30e.js"><link rel="prefetch" href="/assets/js/65.968b9e9f.js"><link rel="prefetch" href="/assets/js/66.1b43f384.js"><link rel="prefetch" href="/assets/js/67.fb067cd5.js"><link rel="prefetch" href="/assets/js/68.94fb1f43.js"><link rel="prefetch" href="/assets/js/69.366f59a5.js"><link rel="prefetch" href="/assets/js/70.9d543737.js"><link rel="prefetch" href="/assets/js/71.c2bbadfb.js"><link rel="prefetch" href="/assets/js/72.d9ad71c6.js"><link rel="prefetch" href="/assets/js/73.fdf1c18d.js"><link rel="prefetch" href="/assets/js/74.817933e2.js"><link rel="prefetch" href="/assets/js/75.1b84ada9.js"><link rel="prefetch" href="/assets/js/76.171fe943.js"><link rel="prefetch" href="/assets/js/77.02471d2d.js"><link rel="prefetch" href="/assets/js/78.9fbacaa9.js"><link rel="prefetch" href="/assets/js/79.3fe97029.js"><link rel="prefetch" href="/assets/js/8.af082f1e.js"><link rel="prefetch" href="/assets/js/80.fde761f4.js"><link rel="prefetch" href="/assets/js/81.77dc188b.js"><link rel="prefetch" href="/assets/js/82.bff1495c.js"><link rel="prefetch" href="/assets/js/83.26ffd6d9.js"><link rel="prefetch" href="/assets/js/84.b2eb10df.js"><link rel="prefetch" href="/assets/js/85.1437d92a.js"><link rel="prefetch" href="/assets/js/86.a388da50.js"><link rel="prefetch" href="/assets/js/87.07f7923d.js"><link rel="prefetch" href="/assets/js/88.c69c5036.js"><link rel="prefetch" href="/assets/js/89.7f885e62.js"><link rel="prefetch" href="/assets/js/9.d6f341ca.js"><link rel="prefetch" href="/assets/js/90.b2147a7f.js"><link rel="prefetch" href="/assets/js/91.b9378968.js"><link rel="prefetch" href="/assets/js/92.c757373d.js"><link rel="prefetch" href="/assets/js/93.05eba063.js"><link rel="prefetch" href="/assets/js/94.b1a94b72.js"><link rel="prefetch" href="/assets/js/95.a5fb7e7e.js"><link rel="prefetch" href="/assets/js/96.9b6870e4.js"><link rel="prefetch" href="/assets/js/97.353656e1.js"><link rel="prefetch" href="/assets/js/98.e3c62cd7.js"><link rel="prefetch" href="/assets/js/99.e58282f2.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.437a03e9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.1b43475e.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-2d5f533b><div data-v-2d5f533b><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-2d5f533b data-v-2d5f533b><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-2d5f533b data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>青灯有味</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Mr.Zhao</span>
            
          <span data-v-64685f0e>2019 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-2d5f533b><header class="navbar" data-v-2d5f533b><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="青灯有味" class="logo"> <span class="site-name">青灯有味</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
      归档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/views/sofeware/" class="nav-link"><i class="iconfont undefined"></i>
  Sofeware
</a></li><li class="dropdown-item"><h4>实战项目</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/vue-manage" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Vue项目
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="/views/demo/" class="nav-link"><i class="iconfont undefined"></i>
  Demo
</a></li><li class="dropdown-subitem"><a href="/views/project.html" class="nav-link"><i class="iconfont undefined"></i>
  我的项目
</a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/rc-bullets/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  弹幕演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  vue3-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-web/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React响应式网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/carland/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React carland
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/blog/" class="nav-link"><i class="iconfont undefined"></i>
  blog
</a></li><li class="dropdown-item"><!----> <a href="/categories/杂集/" class="nav-link"><i class="iconfont undefined"></i>
  杂集
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="/views/theme-example.html" class="nav-link"><i class="iconfont reco-friend"></i>
  友人帐
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-other"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/b7ced0328a6a" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/weixin_42306796" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/5d39b27b51882504b7121ef5" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/zscnb/blog" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask" data-v-2d5f533b></div> <aside class="sidebar" data-v-2d5f533b><div class="personal-info-wrapper" data-v-ca798c94 data-v-2d5f533b><img src="/avatar.gif" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    Mr.Zhao
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>23</h3> <h6 data-v-ca798c94>Article</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>33</h3> <h6 data-v-ca798c94>Tag</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
      归档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/views/sofeware/" class="nav-link"><i class="iconfont undefined"></i>
  Sofeware
</a></li><li class="dropdown-item"><h4>实战项目</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/vue-manage" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Vue项目
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="/views/demo/" class="nav-link"><i class="iconfont undefined"></i>
  Demo
</a></li><li class="dropdown-subitem"><a href="/views/project.html" class="nav-link"><i class="iconfont undefined"></i>
  我的项目
</a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/rc-bullets/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  弹幕演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  vue3-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-web/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React响应式网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/carland/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React carland
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/blog/" class="nav-link"><i class="iconfont undefined"></i>
  blog
</a></li><li class="dropdown-item"><!----> <a href="/categories/杂集/" class="nav-link"><i class="iconfont undefined"></i>
  杂集
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="/views/theme-example.html" class="nav-link"><i class="iconfont reco-friend"></i>
  友人帐
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-other"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/b7ced0328a6a" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/weixin_42306796" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/5d39b27b51882504b7121ef5" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/zscnb/blog" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>久然前端算法扫盲博客</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/views/blog/" aria-current="page" class="sidebar-link">介绍</a></li><li><a href="/views/blog/algorithm.html" aria-current="page" class="active sidebar-link">算法必会篇</a></li><li><a href="/views/blog/design-modes.html" class="sidebar-link">设计模式</a></li><li><a href="/views/blog/leetcode.html" class="sidebar-link">leetcode题库</a></li><li><a href="/views/blog/Sao-operation.html" class="sidebar-link">JavaScript骚操作</a></li><li><a href="/views/blog/Source-code.html" class="sidebar-link">源码解析前端两大框架</a></li><li><a href="/views/blog/Front-end-optimization.html" class="sidebar-link">前端页面优化</a></li><li><a href="/views/blog/compoments.html" class="sidebar-link">组件库开发和发布</a></li><li><a href="/views/blog/react-base.html" class="sidebar-link">React入门到进阶</a></li><li><a href="/views/blog/react-framework.html" class="sidebar-link">React入门篇</a></li><li><a href="/views/blog/react-obtain.html" class="sidebar-link">React就业篇</a></li><li><a href="/views/blog/vue3base.html" class="sidebar-link">Vue3基本教程</a></li><li><a href="/views/blog/vue3study.html" class="sidebar-link">vue3知识梳理</a></li><li><a href="/views/blog/vuestudy.html" class="sidebar-link">学习vue常用知识点</a></li><li><a href="/views/blog/vuex.html" class="sidebar-link">Vuex</a></li><li><a href="/views/blog/Vue_performance_optimization.html" class="sidebar-link">Vue性能优化九法</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>互动3D技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Webpack</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Echarts</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>算法必会篇</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Mr.Zhao</span>
            
          <span data-v-64685f0e>2019 - </span>
          2023
        </a></span></div></div> <div data-v-2d5f533b><main class="page"><div class="page-title" style="display:none;"><h1 class="title">算法必会篇</h1> <div data-v-3b7f5bdf><i class="iconfont reco-account" data-v-3b7f5bdf><span data-v-3b7f5bdf>Mr.Zhao</span></i> <i class="iconfont reco-date" data-v-3b7f5bdf><span data-v-3b7f5bdf>2020-03-06 11:02:43</span></i> <i class="iconfont reco-eye" data-v-3b7f5bdf><span id="/views/blog/algorithm.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-3b7f5bdf><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <!----></div></div> <div class="theme-reco-content content__default" style="display:none;"><div style="margin-bottom:50px;"><div class="container" data-v-67b549a0><div data-v-67b549a0><div class="clip clip1" data-v-67b549a0><div class="content" data-v-67b549a0><h2 style="color: #ec7259" data-v-67b549a0>蒋方舟</h2> <p style="color: #00b77f" data-v-67b549a0>
          一个人如果不看书，他的三观只能由周围的人决定！
        </p></div></div></div><div data-v-67b549a0><div class="clip clip2" data-v-67b549a0><div class="content" data-v-67b549a0><h2 style="color: #ec7259" data-v-67b549a0>苏东坡</h2> <p style="color: #00b77f" data-v-67b549a0>
          有一种潇洒叫，谈笑间樯橹灰飞烟灭。有一豪气叫，会挽雕弓如满月，西北望，射天狼。
        </p></div></div></div><div data-v-67b549a0><div class="clip clip3" data-v-67b549a0><div class="content" data-v-67b549a0><h2 style="color: #ec7259" data-v-67b549a0>白岩松</h2> <p style="color: #00b77f" data-v-67b549a0>
           阳光正好，微风不燥，不负美好时光。所得、所不得，皆不如心安理得，总有不期而遇的温暖!
        </p></div></div></div></div></div> <div id="clock02" data-v-282620c5><span class="date02" data-v-282620c5></span> <span class="time02" data-v-282620c5></span></div> <p>起初神创造天地。地是空虚混沌，渊面黑暗；神的灵运行在水面上。神说：“要有光”。就有了光。神看光是好的，就把光与暗分开了。神称光为昼， 暗为夜。有晚上，有早晨，这是头一日。</p> <p>......</p> <p>神就照着自 己的形象造人，</p> <p>......</p> <p>神说：“看哪！我将遍地上一切结种子的菜蔬，和一切树上所结有核的果子，全赐给你们作食物。至于地上的走兽和空中的飞鸟，以及各样爬在地上有生命的物，我将青草赐给它们作食物”。事就这样成了。</p> <p>神看着一切所造的都甚好。有晚上，有早晨，是第6日。天地万物都造齐了。</p> <p>图1  米开朗基罗创作的西斯廷教堂穹顶画《创世纪》。这幅画里隐含着算法6天</p> <p style="font-size:0;"><img src="/创世纪.jpg" width="100%" height="200px"> <img src="/22.jpg" width="100%" height="200px"></p> <p>圣经上写着：神6天创造天地万有，第7日安歇。</p> <p>对于神创论者来说，这是不可怀疑的事实。但对于进化论者来说，6天创造一切根本就不可能。</p> <p>作为一门讲算法博客，我们当然不打算加入到神创论者和进化论者的永无休止的争论当中去。我们关心的是这么一个问题：圣经上为什么给出的是6天，而不是其他的时间长度。不管是神创论者还是进化论者，弄清楚6这个数字的来历很可能会对己方的观点有所帮助。在这6天里，神将他的创作方程式重复了6次，每天1次。对于全能的神来说，他完全可以在1天、1秒或者任何他所愿意的时间长度里创造天地万物，但却为什么是不多不少的6天呢？而不管圣经上的 “1天”是多长，这个问题都是值得讨论的。</p> <p>我们知道，任何一个自然数的约数中都有1和它本身，而所有小于它本身的因数叫做这个自然数的真约数。例如，6的所有真约数是1、2、3；8的真约数是1、2、4。如果一个数的真约数之和等于这个自然数本身，则这个自然数就称为完全数，或者完美数。例如，6 = 1+2+3，因此6是完美数；而8 ( 1+2+4，因此8不是完美数。<strong style="color:#00b77f;">因此，神6天创造世界，暗示着该创造是完美的！</strong></p> <p>以完美数来昭示创造的完美，似乎合情合理。但问题是，完美数只有6这一个数吗？如果不是，为什么不使用其他的完美数呢？答案是，完美数虽然不止有6这一个，但确实数量稀少。一直到现在（2009年6月），数学家们探索了2600年，并且现代数学家们还借助了超级计算机，但也仅仅找到了47个完美数。其中第1个完美数是6，接下来的4个完美数分别是： 28、496、8128、33 550 336。而第47个完美数有25 956 377个数位，（注意，是数位，不是数值！）它的数值为：243 112 608 × (243 112 609 ? 1)。</p> <p></p><div class="table-of-contents"><ul><li><a href="#数据结构与算法">数据结构与算法</a><ul><li><a href="#数据结构和算法有什么关系">数据结构和算法有什么关系？</a></li></ul></li><li><a href="#一维数据结构">一维数据结构：</a></li><li><a href="#数组">数组：</a></li><li><a href="#链表">链表</a></li><li><a href="#线性数据结构的遍历">线性数据结构的遍历</a></li><li><a href="#线性数据结构的递归遍历">线性数据结构的递归遍历</a></li><li><a href="#链表的逆置">链表的逆置</a></li><li><a href="#经典算法">经典算法</a></li><li><a href="#冒泡排序">冒泡排序</a></li><li><a href="#选择排序">选择排序</a></li><li><a href="#简单快速排序">简单快速排序</a></li><li><a href="#标准快排">标准快排</a></li><li><a href="#栈">栈</a></li><li><a href="#队列">队列</a></li><li><a href="#双向链表">双向链表</a></li><li><a href="#二维数据结构">二维数据结构</a></li><li><a href="#树形结构-文件夹排列">树形结构（文件夹排列）</a></li><li><a href="#二叉树">二叉树</a></li><li><a href="#满二叉树">满二叉树</a></li><li><a href="#完全二叉树">完全二叉树</a></li><li><a href="#子树">子树</a></li><li><a href="#二叉树遍历">二叉树遍历</a></li><li><a href="#前序遍历">前序遍历</a></li><li><a href="#中序遍历">中序遍历</a></li><li><a href="#后序遍历">后序遍历</a></li><li><a href="#代码实现前序中序还原二叉树">代码实现前序中序还原二叉树</a></li><li><a href="#代码实现后序中序还原二叉树">代码实现后序中序还原二叉树</a></li><li><a href="#二叉树的深搜和广搜">二叉树的深搜和广搜</a></li><li><a href="#代码实现二叉树的深度优先搜索-dfs">代码实现二叉树的深度优先搜索(DFS)</a></li><li><a href="#代码实现二叉树的广度优先搜索-bfs">代码实现二叉树的广度优先搜索(BFS)</a></li><li><a href="#二叉树的比较">二叉树的比较</a></li><li><a href="#二叉树左右子树互换后的比较">二叉树左右子树互换后的比较</a></li><li><a href="#二叉树diff算法">二叉树diff算法</a></li><li><a href="#图的最小生成树问题">图的最小生成树问题</a></li><li><a href="#普利姆算法-prime-加点法">普利姆算法(Prime)(加点法)</a></li><li><a href="#克鲁斯卡尔算法-kruskal-加边法">克鲁斯卡尔算法(Kruskal)(加边法)</a></li><li><a href="#代码实现普利姆-prime-算法-加点法">代码实现普利姆(Prime)算法(加点法)</a></li><li><a href="#代码实现克鲁斯卡尔-kruskal-算法-加边法">代码实现克鲁斯卡尔(Kruskal)算法(加边法)</a></li><li><a href="#二叉搜索树">二叉搜索树</a></li><li><a href="#构建二叉搜索树">构建二叉搜索树</a></li><li><a href="#平衡二叉树">平衡二叉树</a></li><li><a href="#用代码判断平衡二叉树">用代码判断平衡二叉树</a></li><li><a href="#二叉树的单旋">二叉树的单旋</a></li><li><a href="#二叉树双旋的概念">二叉树双旋的概念</a></li><li><a href="#二叉树双旋的代码实现">二叉树双旋的代码实现</a></li><li><a href="#左左双旋与右右双旋">左左双旋与右右双旋</a></li><li><a href="#_234树的由来">234树的由来</a></li><li><a href="#树的深度优先搜索">树的深度优先搜索</a></li><li><a href="#树的广度优先搜索">树的广度优先搜索</a></li><li><a href="#图的深度优先搜索">图的深度优先搜索</a></li><li><a href="#图的广度优先搜索">图的广度优先搜索</a></li><li><a href="#动态规划之斐波那契数列">动态规划之斐波那契数列</a></li><li><a href="#动态规划之青蛙跳台阶问题">动态规划之青蛙跳台阶问题</a></li><li><a href="#动态规划之变态青蛙跳台阶问题">动态规划之变态青蛙跳台阶问题</a></li></ul></div><p></p> <h2 id="数据结构与算法"><a href="#数据结构与算法" class="header-anchor">#</a> 数据结构与算法</h2> <h3 id="数据结构和算法有什么关系"><a href="#数据结构和算法有什么关系" class="header-anchor">#</a> 数据结构和算法有什么关系？</h3> <ol><li><p>可以<code>容纳数据的结构</code>被称为数据结构</p></li> <li><p>算法是用来对数据结构进行<code>处理的方法</code></p></li> <li><p>数据结构是静态的。</p></li> <li><p>算法是动态的</p></li></ol> <h2 id="一维数据结构"><a href="#一维数据结构" class="header-anchor">#</a> 一维数据结构：</h2> <p>(线性数据结构：数组、链表)</p> <p>线性的数据结构强调<code>存储与顺序</code></p> <h2 id="数组"><a href="#数组" class="header-anchor">#</a> 数组：</h2> <h5 id="数组特性"><a href="#数组特性" class="header-anchor">#</a> 数组特性：</h5> <p>1.存储在物理空间上是连续的。
2. 底层的数据长度是不可变的。</p> <h5 id="数组优点-查询性能好。指定查询某个位置"><a href="#数组优点-查询性能好。指定查询某个位置" class="header-anchor">#</a> 数组优点： 查询性能好。指定查询某个位置</h5> <p>数组a = {1, 2, 3, 4, 5, 6, 7 ,8}</p> <p><code>a[1], a[2], a[3] 方括号表示存储地址的偏移</code></p> <p><code>操作系统小知识： 通过偏移查询数据性能最好</code></p> <h5 id="数组缺点"><a href="#数组缺点" class="header-anchor">#</a> 数组缺点：</h5> <ul><li><p>因为空间必须是连续的，所以如果数组比较大，当系统的空间碎片较多的时候，容易存不下</p></li> <li><p>因为数组的长度是固定的，所以数组的内容难以被添加和删除</p></li></ul> <h2 id="链表"><a href="#链表" class="header-anchor">#</a> 链表</h2> <p>我想传递一个链表，我必须传递链表的根节点</p> <p><code>每一个节点，都认为自己是根节点</code></p> <p>链表的特点：</p> <ol><li>空间上不上连续的。</li> <li>每存放一个值，都要多开销一个引用空间</li></ol> <p>优点：</p> <ol><li>只要内存足够大，就能存的下，不用担心空间碎片的问题</li> <li>链表的添加和删除非常的容易</li></ol> <p>缺点：</p> <ol><li>查询速度慢（指的查询某个位置）</li> <li>链表每一个节点都需要创建一个指向next的引用，浪费一些空间</li></ol> <hr> <h2 id="线性数据结构的遍历"><a href="#线性数据结构的遍历" class="header-anchor">#</a> 线性数据结构的遍历</h2> <p><code>遍历</code>： 将一个集合中每一个元素进行获取并查看</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 数组遍历</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>

<span class="token keyword">function</span> <span class="token function">bianArr</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 算法必须有严谨性判断，否则没分</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// bianArr(arr)</span>

<span class="token comment">// 链表遍历</span>
<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> node1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>

node1<span class="token punctuation">.</span>next <span class="token operator">=</span> node2
node2<span class="token punctuation">.</span>next <span class="token operator">=</span> node3
node3<span class="token punctuation">.</span>next <span class="token operator">=</span> node4
node4<span class="token punctuation">.</span>next <span class="token operator">=</span> node5

<span class="token keyword">function</span> <span class="token function">bianLink</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> temp <span class="token operator">=</span> root
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>temp <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>temp<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        temp <span class="token operator">=</span> temp<span class="token punctuation">.</span>next
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
<span class="token function">bianLink</span><span class="token punctuation">(</span>node1<span class="token punctuation">)</span>
</code></pre></div><hr> <h2 id="线性数据结构的递归遍历"><a href="#线性数据结构的递归遍历" class="header-anchor">#</a> 线性数据结构的递归遍历</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 数组递归遍历</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>

<span class="token keyword">function</span> <span class="token function">bianArr</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 算法必须有严谨性判断，否则没分</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> i<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token function">bianArr</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>


<span class="token punctuation">}</span>
<span class="token function">bianArr</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>

<span class="token comment">// 链表递归遍历</span>
<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> node1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>

node1<span class="token punctuation">.</span>next <span class="token operator">=</span> node2
node2<span class="token punctuation">.</span>next <span class="token operator">=</span> node3
node3<span class="token punctuation">.</span>next <span class="token operator">=</span> node4
node4<span class="token punctuation">.</span>next <span class="token operator">=</span> node5

<span class="token comment">// 递归遍历，先找出口，就是return值，后递归</span>
<span class="token keyword">function</span> <span class="token function">bianLink</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找出口</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>

    <span class="token comment">//递归</span>
    <span class="token function">bianLink</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>next<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">bianLink</span><span class="token punctuation">(</span>node1<span class="token punctuation">)</span>
</code></pre></div><hr> <h2 id="链表的逆置"><a href="#链表的逆置" class="header-anchor">#</a> 链表的逆置</h2> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> node1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>

node1<span class="token punctuation">.</span>next <span class="token operator">=</span> node2
node2<span class="token punctuation">.</span>next <span class="token operator">=</span> node3
node3<span class="token punctuation">.</span>next <span class="token operator">=</span> node4
node4<span class="token punctuation">.</span>next <span class="token operator">=</span> node5

<span class="token comment">// 链表的逆置 关键点找到最后一个元素</span>
<span class="token keyword">function</span> <span class="token function">nizhi</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 出口,找到最后一个节点为null</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//代表当前节点是倒数第二个节点</span>
        root<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next <span class="token operator">=</span> root <span class="token comment">//让最后一个节点指向自己（倒数第二个节点）</span>
        <span class="token keyword">return</span> root<span class="token punctuation">.</span>next
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 递归</span>
        <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">nizhi</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>next<span class="token punctuation">)</span>
        root<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next <span class="token operator">=</span> root
         <span class="token comment">// 把第一个节点指向null</span>
        root<span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token keyword">null</span>
        <span class="token keyword">return</span> result
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> newRoot <span class="token operator">=</span> <span class="token function">nizhi</span><span class="token punctuation">(</span>node1<span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">bianLink</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token function">bianLink</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>next<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">bianLink</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span>
</code></pre></div><h2 id="经典算法"><a href="#经典算法" class="header-anchor">#</a> 经典算法</h2> <hr> <h2 id="冒泡排序"><a href="#冒泡排序" class="header-anchor">#</a> 冒泡排序</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">buddleSort</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">-</span> i<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">compare</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> arr<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> j<span class="token punctuation">,</span> j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">buddleSort</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">//[ 1, 2, 3, 4, 5, 6, 7, 8 ]</span>

<span class="token keyword">function</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator">&gt;</span> b<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token comment">//大于正序 小于降序</span>
    <span class="token keyword">else</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> temp <span class="token operator">=</span> arr<span class="token punctuation">[</span>a<span class="token punctuation">]</span>
    arr<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> arr<span class="token punctuation">[</span>b<span class="token punctuation">]</span>
    arr<span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">=</span> temp
<span class="token punctuation">}</span>
</code></pre></div><hr> <h2 id="选择排序"><a href="#选择排序" class="header-anchor">#</a> 选择排序</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">selectSort</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> minIndex <span class="token operator">=</span> i
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> j <span class="token operator">=</span> i<span class="token punctuation">;</span> j <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">compare</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> arr<span class="token punctuation">[</span>minIndex<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                minIndex <span class="token operator">=</span> j
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> i<span class="token punctuation">,</span> minIndex<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">selectSort</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token comment">//[ 1, 2, 3, 4, 5, 6, 7, 8 ]</span>

<span class="token keyword">function</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator">&lt;</span> b<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>
    <span class="token keyword">else</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> temp <span class="token operator">=</span> arr<span class="token punctuation">[</span>a<span class="token punctuation">]</span>
    arr<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> arr<span class="token punctuation">[</span>b<span class="token punctuation">]</span>
    arr<span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">=</span> temp
<span class="token punctuation">}</span>
</code></pre></div><p><code>任何一种排序算法，都没有优劣之分，只有是否适合的场景</code></p> <p><code>数据量大（越混乱）的最适于快速排序</code></p> <p><code>越有序适于冒泡排序</code></p> <hr> <h2 id="简单快速排序"><a href="#简单快速排序" class="header-anchor">#</a> 简单快速排序</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 简单快排 使用数组</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">simplyQuickSort</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> leader <span class="token operator">=</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

    <span class="token comment">// 如果传入很大的数组，性能就差了</span>
    <span class="token keyword">var</span> left <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> right <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&lt;</span> leader<span class="token punctuation">)</span> left<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">else</span> right<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    left <span class="token operator">=</span> <span class="token function">simplyQuickSort</span><span class="token punctuation">(</span>left<span class="token punctuation">)</span>
    right <span class="token operator">=</span> <span class="token function">simplyQuickSort</span><span class="token punctuation">(</span>right<span class="token punctuation">)</span>

    <span class="token keyword">return</span> left<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>leader<span class="token punctuation">,</span> right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">simplyQuickSort</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//[ 1, 2, 3, 4, 5, 6, 7, 8 ]</span>
</code></pre></div><hr> <h2 id="标准快排"><a href="#标准快排" class="header-anchor">#</a> 标准快排</h2> <p><code>通过指针判断，不使用数组</code></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 标准快排</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">quick</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> begin<span class="token punctuation">,</span> end</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>begin <span class="token operator">&gt;=</span> end <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token keyword">let</span> left <span class="token operator">=</span> begin
    <span class="token keyword">let</span> right <span class="token operator">=</span> end

    <span class="token keyword">do</span> <span class="token punctuation">{</span>

        <span class="token keyword">do</span> left<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right <span class="token operator">&amp;&amp;</span> arr<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">&lt;</span> arr<span class="token punctuation">[</span>begin<span class="token punctuation">]</span><span class="token punctuation">)</span>

        <span class="token keyword">do</span> right<span class="token operator">--</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>right <span class="token operator">&gt;</span> left <span class="token operator">&amp;&amp;</span> arr<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">&gt;</span> arr<span class="token punctuation">[</span>begin<span class="token punctuation">]</span><span class="token punctuation">)</span>

        <span class="token comment">// arr[left]是左边第一个比leader（arr[begin]）大</span>
        <span class="token comment">// arr[right]是右边第一个比leader（arr[begin]）小</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right<span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> left<span class="token punctuation">,</span> right<span class="token punctuation">)</span>

    <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;</span> right<span class="token punctuation">)</span>

    <span class="token comment">// 找到左右两指针，中间点如下4</span>
    <span class="token keyword">let</span> swapPoint <span class="token operator">=</span> right <span class="token operator">==</span> left <span class="token operator">?</span> right <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">:</span> right<span class="token punctuation">;</span>

    <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> begin<span class="token punctuation">,</span> swapPoint<span class="token punctuation">)</span>

    <span class="token function">quick</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> begin<span class="token punctuation">,</span> swapPoint<span class="token punctuation">)</span>
    <span class="token function">quick</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> swapPoint <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> end<span class="token punctuation">)</span>
    <span class="token keyword">return</span> arr
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> temp <span class="token operator">=</span> arr<span class="token punctuation">[</span>a<span class="token punctuation">]</span>
    arr<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> arr<span class="token punctuation">[</span>b<span class="token punctuation">]</span>
    arr<span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">=</span> temp
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">quickSort</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>arr <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">quick</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">quickSort</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//[ 1, 2, 2, 3, 4, 5, 6, 7, 8 ]</span>
</code></pre></div><hr> <h2 id="栈"><a href="#栈" class="header-anchor">#</a> 栈</h2> <p>栈结构： 先入后出，类比成一个箱子，先放进去的，就被压到下面</p> <p>类比变量声明，全局变量在下面，局部变量在上面</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Stack</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">push</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">pop</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>arr<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Stack</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

s<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
s<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
s<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span>arr<span class="token punctuation">)</span> <span class="token comment">//[ 1, 2, 3 ]</span>
s<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span>arr<span class="token punctuation">)</span> <span class="token comment">//[ 1, 2 ]</span>
</code></pre></div><hr> <h2 id="队列"><a href="#队列" class="header-anchor">#</a> 队列</h2> <p>队列结构：先入先出，类比成一个管道</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">enqueue</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">dequeue</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>arr<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> q <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

q<span class="token punctuation">.</span><span class="token function">enqueue</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
q<span class="token punctuation">.</span><span class="token function">enqueue</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
q<span class="token punctuation">.</span><span class="token function">enqueue</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span>
q<span class="token punctuation">.</span><span class="token function">enqueue</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>q<span class="token punctuation">.</span>arr<span class="token punctuation">)</span> <span class="token comment">//[ 4, 5, 6, 7 ]</span>
q<span class="token punctuation">.</span><span class="token function">dequeue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>q<span class="token punctuation">.</span>arr<span class="token punctuation">)</span> <span class="token comment">//[ 5, 6, 7 ]</span>
</code></pre></div><hr> <h2 id="双向链表"><a href="#双向链表" class="header-anchor">#</a> 双向链表</h2> <p>优点：无论给出哪一个节点，都能对整个链表进行遍历</p> <p>缺点： 多耗费一个引用的空间，而且构建双向链表比较复杂</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>prev <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> node1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> node3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> node4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>

node1<span class="token punctuation">.</span>next <span class="token operator">=</span> node2
node2<span class="token punctuation">.</span>prev <span class="token operator">=</span> node1
node2<span class="token punctuation">.</span>next <span class="token operator">=</span> node3
node3<span class="token punctuation">.</span>prev <span class="token operator">=</span> node2
node3<span class="token punctuation">.</span>next <span class="token operator">=</span> node4
node4<span class="token punctuation">.</span>prev <span class="token operator">=</span> node3
node4<span class="token punctuation">.</span>next <span class="token operator">=</span> node5
node5<span class="token punctuation">.</span>prev <span class="token operator">=</span> node4

<span class="token keyword">function</span> <span class="token function">bianli</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token function">bianli</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>next<span class="token punctuation">)</span>
        <span class="token comment">// bianli(root.prev)</span>

<span class="token punctuation">}</span>
<span class="token function">bianli</span><span class="token punctuation">(</span>node1<span class="token punctuation">)</span>
</code></pre></div><hr> <h2 id="二维数据结构"><a href="#二维数据结构" class="header-anchor">#</a> 二维数据结构</h2> <p>类比文件夹排列方式</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token comment">//创建4行8列的数组</span>
</code></pre></div><p>二维数据结构是一维数据结构进化</p> <p>二维拓扑结构(图) 是 链表进化</p> <hr> <h2 id="树形结构-文件夹排列"><a href="#树形结构-文件夹排列" class="header-anchor">#</a> 树形结构（文件夹排列）</h2> <p><strong>—— 有向无环图 —— 树是图的一种</strong></p> <p>树形结构有一个跟节点（比如C盘）</p> <p>树形结构没有回路(闭合路径)</p> <p><img src="/assets/img/1.94d74847.png" alt="image"></p> <p>根节点： A</p> <p>叶子节点：下边没有其他节点了的节点</p> <p>节点：既不是根节点，又不是叶子节点的普通节点</p> <p>树的度： 这颗树有最多叉的节点有多少个叉，就有多少个度，如图为3</p> <p><code>树的深度： 树最深有几层，树的深度就为几</code>，如图为3</p> <hr> <h2 id="二叉树"><a href="#二叉树" class="header-anchor">#</a> 二叉树</h2> <p>树的度最多为2的树形结构</p> <p><img src="" alt="image"></p> <ol><li>二叉树的根节点A</li> <li>子节点：某个节点下面的节点</li> <li>父节点： 上级节点</li> <li>叶子节点： CDE</li> <li>节点： B</li></ol> <hr> <h2 id="满二叉树"><a href="#满二叉树" class="header-anchor">#</a> 满二叉树</h2> <p><img src="/assets/img/3.b1d98978.png" alt="image"></p> <p>(1) 所有的叶子节点都在最底层</p> <p>(2) 每个非叶子节点都有两个子节点（必须有两个子节点）</p> <hr> <h2 id="完全二叉树"><a href="#完全二叉树" class="header-anchor">#</a> 完全二叉树</h2> <p>国内定义：</p> <ol><li>叶子节点都在最后一层或倒数第二层</li> <li>叶子节点都向左聚拢</li></ol> <p><img src="" alt="image"></p> <p><img src="" alt="image"></p> <p><img src="/assets/img/6.152389d1.png" alt="image"></p> <p>国际定义：</p> <ol><li>叶子节点都在最后一层或倒数第二层</li> <li>如果有叶子节点，就必然有两个叶子节点</li></ol> <p><img src="" alt="image"></p> <p><img src="/assets/img/8.9ad028b5.png" alt="image"></p> <p><img src="/assets/img/9.086e934b.png" alt="image"></p> <hr> <h2 id="子树"><a href="#子树" class="header-anchor">#</a> 子树</h2> <p><code>在二叉树中，每个节点都认为自己是根节点</code></p> <p>子树： 二叉树中，每一个节点(包括树的根节点)或叶子节点，都是一颗子树的根节点</p> <p>左子树 和 右子树：</p> <p>对于A来说</p> <p><img src="/assets/img/10.58fba363.png" alt="image"></p> <hr> <h2 id="二叉树遍历"><a href="#二叉树遍历" class="header-anchor">#</a> 二叉树遍历</h2> <p><code>遍历</code>: 将一个集合中每一个元素进行获取并查看</p> <p>传递二叉树(链表)要传根节点</p> <p><code>前序遍历：(先根次序遍历)</code></p> <blockquote><p>先打印当前的，再打印左边的，再打印右边的</p></blockquote> <p><code>中序遍历：(中根次序遍历)</code></p> <blockquote><p>先打印左边的，再打印当前的，再打印右边的</p></blockquote> <p><code>后序遍历：(后根次序遍历)</code></p> <blockquote><p>先打印左边的，再打印右边的，再打印当前的</p></blockquote> <p><img src="/assets/img/11.f581e3bb.png" alt="image"></p> <p>前序遍历：ACFGBDE(A在最前面)</p> <p>中序遍历(次序投影)：FCGADBE(A在中间)</p> <p>后序遍历：FGCDEBA(A在最后面)</p> <hr> <h2 id="前序遍历"><a href="#前序遍历" class="header-anchor">#</a> 前序遍历</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 前序遍历</span>
<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;B&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;C&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;D&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;E&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;F&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;G&quot;</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>left <span class="token operator">=</span> c
a<span class="token punctuation">.</span>right <span class="token operator">=</span> b
c<span class="token punctuation">.</span>left <span class="token operator">=</span> f
c<span class="token punctuation">.</span>right <span class="token operator">=</span> g
b<span class="token punctuation">.</span>left <span class="token operator">=</span> d
b<span class="token punctuation">.</span>right <span class="token operator">=</span> e

<span class="token comment">// 先根次序遍历</span>
<span class="token keyword">function</span> <span class="token function">forwards</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>

    <span class="token function">forwards</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

    <span class="token function">forwards</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">forwards</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token comment">//传入根节点  输出 ACFGBDE</span>
</code></pre></div><hr> <h2 id="中序遍历"><a href="#中序遍历" class="header-anchor">#</a> 中序遍历</h2> <p><strong>(次序的投影)</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 中序遍历</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;B&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;C&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;D&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;E&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;F&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;G&quot;</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>left <span class="token operator">=</span> c
a<span class="token punctuation">.</span>right <span class="token operator">=</span> b
c<span class="token punctuation">.</span>left <span class="token operator">=</span> f
c<span class="token punctuation">.</span>right <span class="token operator">=</span> g
b<span class="token punctuation">.</span>left <span class="token operator">=</span> d
b<span class="token punctuation">.</span>right <span class="token operator">=</span> e

<span class="token comment">// 中根次序遍历</span>
<span class="token keyword">function</span> <span class="token function">median</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token function">median</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>

    <span class="token function">median</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token function">median</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">//传入根节点   输出 FCGADBE</span>
</code></pre></div><hr> <h2 id="后序遍历"><a href="#后序遍历" class="header-anchor">#</a> 后序遍历</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 后序遍历</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;B&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;C&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;D&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;E&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;F&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">&quot;G&quot;</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>left <span class="token operator">=</span> c
a<span class="token punctuation">.</span>right <span class="token operator">=</span> b
c<span class="token punctuation">.</span>left <span class="token operator">=</span> f
c<span class="token punctuation">.</span>right <span class="token operator">=</span> g
b<span class="token punctuation">.</span>left <span class="token operator">=</span> d
b<span class="token punctuation">.</span>right <span class="token operator">=</span> e

<span class="token comment">// 后根次序遍历</span>

<span class="token keyword">function</span> <span class="token function">backwards</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token function">backwards</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

    <span class="token function">backwards</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token function">backwards</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">//传入根节点    输出 FGCDEBA</span>
</code></pre></div><hr> <h4 id="给出前序中序还原二叉树-必须有中序-否则还原不了-并写出后序遍历"><a href="#给出前序中序还原二叉树-必须有中序-否则还原不了-并写出后序遍历" class="header-anchor">#</a> 给出前序中序还原二叉树(必须有中序，否则还原不了)，并写出后序遍历</h4> <p>前序遍历：A(根节点)  CFG(左子树) BDE(右子树)</p> <p>中序遍历：(左子树)FCG A(根节点) DBE(右子树)</p> <p>如下图所示：</p> <p><img src="" alt="image"></p> <p>故后序遍历： FGCDEBA</p> <hr> <h4 id="给出后序中序还原二叉树-必须有中序-否则还原不了-并写出前序遍历"><a href="#给出后序中序还原二叉树-必须有中序-否则还原不了-并写出前序遍历" class="header-anchor">#</a> 给出后序中序还原二叉树(必须有中序，否则还原不了)，并写出前序遍历</h4> <p>后序遍历： (左子树)FGC  DEB(左子树) A(根节点)</p> <p>中序遍历：(左子树)FCG A(根节点) DBE(左子树)</p> <p>如下图所示：</p> <p><img src="" alt="image"></p> <p>故前序遍历： ACFGBDE</p> <hr> <h2 id="代码实现前序中序还原二叉树"><a href="#代码实现前序中序还原二叉树" class="header-anchor">#</a> 代码实现前序中序还原二叉树</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 代码实现前序中序还原二叉树</span>
<span class="token keyword">var</span> pre <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'F'</span><span class="token punctuation">,</span> <span class="token string">'G'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'D'</span><span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">]</span>
<span class="token keyword">var</span> mid <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'F'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'G'</span><span class="token punctuation">,</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'D'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">]</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">preMid</span><span class="token punctuation">(</span><span class="token parameter">pre<span class="token punctuation">,</span> mid</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>pre <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> mid <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> pre<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> mid<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> pre<span class="token punctuation">.</span>length <span class="token operator">!=</span> mid<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">// 获取根节点</span>
    <span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>pre<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token comment">// 获取根节点在中序的索引位置</span>
    <span class="token keyword">var</span> index <span class="token operator">=</span> mid<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>

    <span class="token comment">// 获取前序左子树</span>
    <span class="token keyword">var</span> preLeft <span class="token operator">=</span> pre<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> index <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token comment">// 获取前序右子树</span>
    <span class="token keyword">var</span> preRight <span class="token operator">=</span> pre<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>index <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token comment">// 获取中序的左子树</span>
    <span class="token keyword">var</span> midLeft <span class="token operator">=</span> mid<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span>

    <span class="token comment">// 获取中序右子树</span>
    <span class="token keyword">var</span> midRight <span class="token operator">=</span> mid<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>index <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token comment">// 根据左子树的前序和中序还原左子树并赋值给root.left</span>
    root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">preMid</span><span class="token punctuation">(</span>preLeft<span class="token punctuation">,</span> midLeft<span class="token punctuation">)</span>

    <span class="token comment">// 根据右子树的前序和中序还原右子树并赋值给root.right</span>
    root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">preMid</span><span class="token punctuation">(</span>preRight<span class="token punctuation">,</span> midRight<span class="token punctuation">)</span>
    
    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">preMid</span><span class="token punctuation">(</span>pre<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><hr> <h2 id="代码实现后序中序还原二叉树"><a href="#代码实现后序中序还原二叉树" class="header-anchor">#</a> 代码实现后序中序还原二叉树</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 代码实现后序中序还原二叉树</span>

<span class="token keyword">var</span> after <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'F'</span><span class="token punctuation">,</span> <span class="token string">'G'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'D'</span><span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'A'</span><span class="token punctuation">]</span>
<span class="token keyword">var</span> mid <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'F'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'G'</span><span class="token punctuation">,</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'D'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">]</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">afterMid</span><span class="token punctuation">(</span><span class="token parameter">after<span class="token punctuation">,</span> mid</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>after <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> mid <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> after<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> mid<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> after<span class="token punctuation">.</span>length <span class="token operator">!=</span> mid<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">// 获取根节点</span>
    <span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>after<span class="token punctuation">[</span>after<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token comment">// 获取根节点在中序的位置</span>
    <span class="token keyword">var</span> index <span class="token operator">=</span> mid<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>

    <span class="token comment">// 获取后序左子树</span>
    <span class="token keyword">var</span> afterLeft <span class="token operator">=</span> after<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span>

    <span class="token comment">// 获取后序右子树</span>
    <span class="token keyword">var</span> afterRight <span class="token operator">=</span> after<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>index<span class="token punctuation">,</span> after<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token comment">// 获取中序左子树</span>
    <span class="token keyword">var</span> midLeft <span class="token operator">=</span> mid<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span>

    <span class="token comment">// 获取中序右子树</span>
    <span class="token keyword">var</span> midRight <span class="token operator">=</span> mid<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>index <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token comment">// 根据左子树的后序和中序还原左子树并赋值给root.left</span>
    root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">afterMid</span><span class="token punctuation">(</span>afterLeft<span class="token punctuation">,</span> midLeft<span class="token punctuation">)</span>

    <span class="token comment">// 根据右子树的后序和中序还原右子树并赋值给root.right</span>
    root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">afterMid</span><span class="token punctuation">(</span>afterRight<span class="token punctuation">,</span> midRight<span class="token punctuation">)</span>

    <span class="token keyword">return</span> root

<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">afterMid</span><span class="token punctuation">(</span>after<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><hr> <h2 id="二叉树的深搜和广搜"><a href="#二叉树的深搜和广搜" class="header-anchor">#</a> 二叉树的深搜和广搜</h2> <p>二叉树的搜索</p> <p>树的搜索，图的搜索，爬虫的逻辑，搜索引擎的爬虫算法</p> <p><code>深度优先搜索：更适合探索未知</code></p> <p><code>广度优先搜索：更适合探索局域</code></p> <hr> <h2 id="代码实现二叉树的深度优先搜索-dfs"><a href="#代码实现二叉树的深度优先搜索-dfs" class="header-anchor">#</a> 代码实现二叉树的深度优先搜索(DFS)</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>left <span class="token operator">=</span> c
a<span class="token punctuation">.</span>right <span class="token operator">=</span> b
c<span class="token punctuation">.</span>left <span class="token operator">=</span> f
c<span class="token punctuation">.</span>right <span class="token operator">=</span> g
b<span class="token punctuation">.</span>left <span class="token operator">=</span> d
b<span class="token punctuation">.</span>right <span class="token operator">=</span> e

<span class="token comment">// 代码实现二叉树的深度优先搜索</span>
<span class="token comment">// 对于二叉树来说，深度优先搜索和前序遍历的顺序是一样的</span>
<span class="token keyword">function</span> <span class="token constant">DFS</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">var</span> left <span class="token operator">=</span> <span class="token constant">DFS</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> target<span class="token punctuation">)</span>

    <span class="token keyword">var</span> right <span class="token operator">=</span> <span class="token constant">DFS</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> target<span class="token punctuation">)</span>

    <span class="token keyword">return</span> left <span class="token operator">||</span> right

<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">DFS</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
</code></pre></div><hr> <h2 id="代码实现二叉树的广度优先搜索-bfs"><a href="#代码实现二叉树的广度优先搜索-bfs" class="header-anchor">#</a> 代码实现二叉树的广度优先搜索(BFS)</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>left <span class="token operator">=</span> c
a<span class="token punctuation">.</span>right <span class="token operator">=</span> b
c<span class="token punctuation">.</span>left <span class="token operator">=</span> f
c<span class="token punctuation">.</span>right <span class="token operator">=</span> g
b<span class="token punctuation">.</span>left <span class="token operator">=</span> d
b<span class="token punctuation">.</span>right <span class="token operator">=</span> e

<span class="token comment">// 代码实现二叉树的广度优先搜索(BFS)</span>
<span class="token keyword">function</span> <span class="token constant">BFS</span><span class="token punctuation">(</span><span class="token parameter">rootList<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>rootList <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> rootList<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">var</span> childList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> rootList<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        
        <span class="token comment">// 为防止数组里面出现null而报错</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>rootList<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>rootList<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> rootList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">true</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            childList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>rootList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>left<span class="token punctuation">)</span>
            childList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>rootList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>right<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token constant">BFS</span><span class="token punctuation">(</span>childList<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">BFS</span><span class="token punctuation">(</span><span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'g'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
</code></pre></div><hr> <h2 id="二叉树的比较"><a href="#二叉树的比较" class="header-anchor">#</a> 二叉树的比较</h2> <p><img src="/assets/img/13.8c788342.png" alt="image"></p> <p><code>遇到二叉树比较的问题时，必须要确定，左右两颗子树如果交换位置，既左右互换算不算同一颗二叉树</code></p> <p><code>如果是笔试的话，没有特殊的说明左右互换还是同一颗树，那么默认互换后不是同一棵树</code></p> <p><code>如果是面试。尽量问一下。</code></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a1<span class="token punctuation">.</span>left <span class="token operator">=</span> c1
a1<span class="token punctuation">.</span>right <span class="token operator">=</span> b1
c1<span class="token punctuation">.</span>left <span class="token operator">=</span> f1
c1<span class="token punctuation">.</span>right <span class="token operator">=</span> g1
b1<span class="token punctuation">.</span>left <span class="token operator">=</span> d1
b1<span class="token punctuation">.</span>right <span class="token operator">=</span> e1

<span class="token keyword">var</span> a2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a2<span class="token punctuation">.</span>left <span class="token operator">=</span> c2
a2<span class="token punctuation">.</span>right <span class="token operator">=</span> b2
c2<span class="token punctuation">.</span>left <span class="token operator">=</span> f2
c2<span class="token punctuation">.</span>right <span class="token operator">=</span> g2
b2<span class="token punctuation">.</span>left <span class="token operator">=</span> d2
b2<span class="token punctuation">.</span>right <span class="token operator">=</span> e2

<span class="token comment">// 二叉树的比较</span>

<span class="token comment">// 强调左右不能互换</span>

<span class="token keyword">function</span> <span class="token function">compareTree</span><span class="token punctuation">(</span><span class="token parameter">root1<span class="token punctuation">,</span> root2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 判断是同一颗树</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">==</span> root2<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> root2 <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">||</span> root2 <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> root1 <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 相同位置是否相等</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1<span class="token punctuation">.</span>value <span class="token operator">!=</span> root2<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 左子树与右子树皆为true才证明同一颗树</span>
    <span class="token keyword">return</span> <span class="token function">compareTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>left<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>left<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">compareTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>right<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">compareTree</span><span class="token punctuation">(</span>a1<span class="token punctuation">,</span> a2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
</code></pre></div><hr> <h2 id="二叉树左右子树互换后的比较"><a href="#二叉树左右子树互换后的比较" class="header-anchor">#</a> 二叉树左右子树互换后的比较</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a1<span class="token punctuation">.</span>left <span class="token operator">=</span> c1
a1<span class="token punctuation">.</span>right <span class="token operator">=</span> b1
c1<span class="token punctuation">.</span>left <span class="token operator">=</span> f1
c1<span class="token punctuation">.</span>right <span class="token operator">=</span> g1
b1<span class="token punctuation">.</span>left <span class="token operator">=</span> d1
b1<span class="token punctuation">.</span>right <span class="token operator">=</span> e1

<span class="token keyword">var</span> a2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

<span class="token comment">// 交换位置</span>
a2<span class="token punctuation">.</span>right <span class="token operator">=</span> c2
a2<span class="token punctuation">.</span>left <span class="token operator">=</span> b2

c2<span class="token punctuation">.</span>left <span class="token operator">=</span> f2
c2<span class="token punctuation">.</span>right <span class="token operator">=</span> g2
b2<span class="token punctuation">.</span>left <span class="token operator">=</span> d2
b2<span class="token punctuation">.</span>right <span class="token operator">=</span> e2

<span class="token comment">// 二叉树的比较</span>

<span class="token comment">// 强调左右互换,也可以左右互换</span>

<span class="token keyword">function</span> <span class="token function">compareTree</span><span class="token punctuation">(</span><span class="token parameter">root1<span class="token punctuation">,</span> root2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 判断是同一颗树</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">==</span> root2<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> root2 <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">||</span> root2 <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> root1 <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 相同位置是否相等</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1<span class="token punctuation">.</span>value <span class="token operator">!=</span> root2<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">return</span> <span class="token function">compareTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>left<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>left<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">compareTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>right<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>right<span class="token punctuation">)</span> <span class="token operator">||</span>
        <span class="token function">compareTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>left<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>right<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">compareTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>right<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">compareTree</span><span class="token punctuation">(</span>a1<span class="token punctuation">,</span> a2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
</code></pre></div><hr> <h2 id="二叉树diff算法"><a href="#二叉树diff算法" class="header-anchor">#</a> 二叉树diff算法</h2> <p><img src="/assets/img/14.41830a2b.png" alt="image"></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a1<span class="token punctuation">.</span>left <span class="token operator">=</span> c1
a1<span class="token punctuation">.</span>right <span class="token operator">=</span> b1
c1<span class="token punctuation">.</span>left <span class="token operator">=</span> f1
c1<span class="token punctuation">.</span>right <span class="token operator">=</span> g1
b1<span class="token punctuation">.</span>left <span class="token operator">=</span> d1
b1<span class="token punctuation">.</span>right <span class="token operator">=</span> e1

<span class="token keyword">var</span> a2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>

a2<span class="token punctuation">.</span>right <span class="token operator">=</span> b2
a2<span class="token punctuation">.</span>left <span class="token operator">=</span> c2

c2<span class="token punctuation">.</span>left <span class="token operator">=</span> f2

<span class="token comment">// 删除</span>
<span class="token comment">// c2.right = g2</span>

b2<span class="token punctuation">.</span>left <span class="token operator">=</span> d2
b2<span class="token punctuation">.</span>right <span class="token operator">=</span> e2

<span class="token comment">// 新增</span>
f2<span class="token punctuation">.</span>right <span class="token operator">=</span> g2

<span class="token comment">// 二叉树的diff 比较DOM </span>

<span class="token comment">// 新增了什么，修改了什么，删除了什么</span>

<span class="token comment">// {type:'新增', origin: null, now: c2}</span>
<span class="token comment">// {type:'修改', origin: c1, now: c2}</span>
<span class="token comment">// {type:'删除', origin: c2, now: null}</span>

<span class="token keyword">function</span> <span class="token function">diffTree</span><span class="token punctuation">(</span><span class="token parameter">root1<span class="token punctuation">,</span> root2<span class="token punctuation">,</span> diffList</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 两个节点一样返回diffList 递归出口</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">==</span> root2<span class="token punctuation">)</span> <span class="token keyword">return</span> diffList

    <span class="token comment">// 新增了什么</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> root2 <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        diffList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">'新增'</span><span class="token punctuation">,</span> origin<span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span> now<span class="token operator">:</span> root2 <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 删除了什么</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root1 <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> root2 <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        diffList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">'删除'</span><span class="token punctuation">,</span> origin<span class="token operator">:</span> root1<span class="token punctuation">,</span> now<span class="token operator">:</span> <span class="token keyword">null</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 修改了什么</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root1<span class="token punctuation">.</span>value <span class="token operator">!=</span> root2<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        diffList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">'修改'</span><span class="token punctuation">,</span> origin<span class="token operator">:</span> root1<span class="token punctuation">,</span> now<span class="token operator">:</span> root2 <span class="token punctuation">}</span><span class="token punctuation">)</span>

    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">diffTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>left<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>left<span class="token punctuation">,</span> diffList<span class="token punctuation">)</span>
        <span class="token function">diffTree</span><span class="token punctuation">(</span>root1<span class="token punctuation">.</span>right<span class="token punctuation">,</span> root2<span class="token punctuation">.</span>right<span class="token punctuation">,</span> diffList<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> diffList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token function">diffTree</span><span class="token punctuation">(</span>a1<span class="token punctuation">,</span> a2<span class="token punctuation">,</span> diffList<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>diffList<span class="token punctuation">)</span>

输出：
<span class="token punctuation">[</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">'新增'</span><span class="token punctuation">,</span>
    origin<span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
    now<span class="token operator">:</span> Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'G'</span><span class="token punctuation">,</span> left<span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span> right<span class="token operator">:</span> <span class="token keyword">null</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">'删除'</span><span class="token punctuation">,</span>
    origin<span class="token operator">:</span> Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'G'</span><span class="token punctuation">,</span> left<span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span> right<span class="token operator">:</span> <span class="token keyword">null</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    now<span class="token operator">:</span> <span class="token keyword">null</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
</code></pre></div><hr> <h2 id="图的最小生成树问题"><a href="#图的最小生成树问题" class="header-anchor">#</a> 图的最小生成树问题</h2> <p><img src="/assets/img/15.bc969a20.png" alt="image"></p> <p>树： 有向无环图</p> <p><code>普利姆算法(Prime)(加点法)</code></p> <p><code>克鲁斯卡尔算法(Kruskal)(加边法)</code></p> <hr> <h2 id="普利姆算法-prime-加点法"><a href="#普利姆算法-prime-加点法" class="header-anchor">#</a> 普利姆算法(Prime)(加点法)</h2> <ol><li>任选一个点作为起点</li> <li>找到以当前选中的点为起点路径最短的边</li> <li>如果这个边的另一端没有被连通进来，那么就连结</li> <li>如果这个边的的另一端也早就被连通进来了，则看倒数第二短的边</li> <li>重复2-4直到将所有的点都连通为止</li></ol> <p><img src="/assets/img/16.fbba94ac.png" alt="image"></p> <hr> <h2 id="克鲁斯卡尔算法-kruskal-加边法"><a href="#克鲁斯卡尔算法-kruskal-加边法" class="header-anchor">#</a> 克鲁斯卡尔算法(Kruskal)(加边法)</h2> <ol><li>选择最短的边进行连接</li> <li>要保证边连接的两端至少有一个点是最新的点</li> <li>或者这个边是将两个部落进行连接的</li> <li>重复1-3直到将所有的点都连接到一起</li></ol> <p><img src="/assets/img/16.fbba94ac.png" alt="image"></p> <hr> <h2 id="代码实现普利姆-prime-算法-加点法"><a href="#代码实现普利姆-prime-算法-加点法" class="header-anchor">#</a> 代码实现普利姆(Prime)算法(加点法)</h2> <p>表示一个图，可以使用点集合和边集合</p> <p>点集合：[a, b, c, d, e]</p> <p><img src="/assets/img/17.889e5917.png" alt="image"></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 普利姆(Prime)算法(加点法)</span>
<span class="token keyword">var</span> <span class="token constant">MAX</span> <span class="token operator">=</span> <span class="token number">65535</span>

<span class="token comment">// 点集合</span>
<span class="token keyword">var</span> pointSet <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token comment">// 相邻矩阵(距离)</span>
<span class="token keyword">var</span> distance <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>neighbor <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>

pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">getIndex</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> pointSet<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>str <span class="token operator">==</span> pointSet<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token keyword">return</span> i
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token comment">// 需要传入点的集合，边的集合，当前已经连接进入的集合</span>
<span class="token comment">// 此方法，根据当前已经有的节点，来进行判断，获取到距离最短的点</span>
<span class="token keyword">function</span> <span class="token function">getMinDisNode</span><span class="token punctuation">(</span><span class="token parameter">pointSet<span class="token punctuation">,</span> distance<span class="token punctuation">,</span> nowPointSet</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 线段的起点</span>
    <span class="token keyword">var</span> fromNode <span class="token operator">=</span> <span class="token keyword">null</span>

    <span class="token comment">// 线段的终点</span>
    <span class="token keyword">var</span> minDisNode <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">var</span> minDis <span class="token operator">=</span> <span class="token constant">MAX</span> <span class="token comment">//线段默认距离MAX</span>

    <span class="token comment">// 根据当前已有的这些 点为起点，依次判断连接其他的点的距离是多少</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nowPointSet<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token keyword">var</span> nowPointIndex <span class="token operator">=</span> <span class="token function">getIndex</span><span class="token punctuation">(</span>nowPointSet<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span>

        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> distance<span class="token punctuation">[</span>nowPointIndex<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

            <span class="token comment">// thisNode表示distance中的点，但是这个点不是对象</span>
            <span class="token keyword">var</span> thisNode <span class="token operator">=</span> pointSet<span class="token punctuation">[</span>j<span class="token punctuation">]</span>

            <span class="token comment">// 首先这个点不能是已经接入的点，且点之间的距离得是目前的最短距离</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>nowPointSet<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>thisNode<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> distance<span class="token punctuation">[</span>nowPointIndex<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&lt;</span> minDis<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                fromNode <span class="token operator">=</span> nowPointSet<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
                minDisNode <span class="token operator">=</span> thisNode
                minDis <span class="token operator">=</span> distance<span class="token punctuation">[</span>nowPointIndex<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>minDis<span class="token punctuation">)</span>
        <span class="token comment">// 将起点和终点相连</span>
    fromNode<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>minDisNode<span class="token punctuation">)</span>
    minDisNode<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>fromNode<span class="token punctuation">)</span>
    <span class="token keyword">return</span> minDisNode
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">prime</span><span class="token punctuation">(</span><span class="token parameter">pointSet<span class="token punctuation">,</span> distance<span class="token punctuation">,</span> startPiont</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> nowPointSet <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    nowPointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>startPiont<span class="token punctuation">)</span>

    <span class="token comment">// 获取最小代价的边</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> minDisNode <span class="token operator">=</span> <span class="token function">getMinDisNode</span><span class="token punctuation">(</span>pointSet<span class="token punctuation">,</span> distance<span class="token punctuation">,</span> nowPointSet<span class="token punctuation">)</span>
        nowPointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>minDisNode<span class="token punctuation">)</span>

        <span class="token comment">// 把所有的点连接起来终止</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>nowPointSet<span class="token punctuation">.</span>length <span class="token operator">==</span> pointSet<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


<span class="token function">prime</span><span class="token punctuation">(</span>pointSet<span class="token punctuation">,</span> distance<span class="token punctuation">,</span> pointSet<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>pointSet<span class="token punctuation">)</span>

输出：
<span class="token number">5</span> <span class="token comment">//CD</span>
<span class="token number">6</span> <span class="token comment">//BD</span>
<span class="token number">4</span> <span class="token comment">//AB</span>
<span class="token number">7</span> <span class="token comment">//DE</span>
<span class="token punctuation">[</span> Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'A'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'B'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'C'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'D'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'E'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>

</code></pre></div><hr> <h2 id="代码实现克鲁斯卡尔-kruskal-算法-加边法"><a href="#代码实现克鲁斯卡尔-kruskal-算法-加边法" class="header-anchor">#</a> 代码实现克鲁斯卡尔(Kruskal)算法(加边法)</h2> <p><img src="/assets/img/18.d890616a.png" alt="image"></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 克鲁斯卡尔(Kruskal)算法(加边法)</span>
<span class="token keyword">var</span> <span class="token constant">MAX</span> <span class="token operator">=</span> <span class="token number">65535</span>

<span class="token comment">// 点集合</span>
<span class="token keyword">var</span> pointSet <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token comment">// 相邻矩阵(距离)</span>
<span class="token keyword">var</span> distance <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token constant">MAX</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>neighbor <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>

pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>
pointSet<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">canLink</span><span class="token punctuation">(</span><span class="token parameter">resultList<span class="token punctuation">,</span> tempBegin<span class="token punctuation">,</span> tempEnd</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> beginIn <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">var</span> endIn <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> resultList<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 判断起点，终点是否在这个部落里</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>tempBegin<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            beginIn <span class="token operator">=</span> resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>tempEnd<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            endIn <span class="token operator">=</span> resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// begin和end在同一个部落 =&gt; 不可以连接</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>beginIn <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> endIn <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> beginIn <span class="token operator">==</span> endIn<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 两个点都是新的点(都不在任何部落里) =&gt; 可以连接，产生新的部落</span>
    <span class="token comment">// begin在A部落，end没有部落，  =&gt; A部落扩张一个村庄</span>
    <span class="token comment">// end在A部落，begin没有部落，  =&gt; A部落扩张一个村庄</span>
    <span class="token comment">// begin在A部落，end在B部落，  =&gt; 将AB两个部落合并</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">link</span><span class="token punctuation">(</span><span class="token parameter">resultList<span class="token punctuation">,</span> tempBegin<span class="token punctuation">,</span> tempEnd</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> beginIn <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">var</span> endIn <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> resultList<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 判断起点，终点是否在这个部落里</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>tempBegin<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            beginIn <span class="token operator">=</span> resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>tempEnd<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            endIn <span class="token operator">=</span> resultList<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 两个点都是新的点(都不在任何部落里) =&gt; 可以连接，产生新的部落</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>beginIn <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> endIn <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> newArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        newArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tempBegin<span class="token punctuation">)</span>
        newArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tempEnd<span class="token punctuation">)</span>
        resultList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// begin在A部落，end没有部落，  =&gt; A部落扩张一个村庄</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>beginIn <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> endIn <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        beginIn<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tempEnd<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// end在A部落，begin没有部落，  =&gt; A部落扩张一个村庄</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>beginIn <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> endIn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        endIn<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tempBegin<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// begin在A部落，end在B部落，  =&gt; 将AB两个部落合并</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>beginIn <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> endIn <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> beginIn <span class="token operator">!=</span> endIn<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> allIn <span class="token operator">=</span> beginIn<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>endIn<span class="token punctuation">)</span>
        <span class="token keyword">var</span> needRemove <span class="token operator">=</span> resultList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>endIn<span class="token punctuation">)</span>
        resultList<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>needRemove<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
        needRemove <span class="token operator">=</span> resultList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>beginIn<span class="token punctuation">)</span>
        resultList<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>needRemove<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
        resultList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>allIn<span class="token punctuation">)</span>

    <span class="token punctuation">}</span>

    tempBegin<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tempEnd<span class="token punctuation">)</span>
    tempEnd<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tempBegin<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">Kruskal</span><span class="token punctuation">(</span><span class="token parameter">pointSet<span class="token punctuation">,</span> distance</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">var</span> resultList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token comment">//这里是二维数组，此数组代表的是有多少个‘部落’</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> minDis <span class="token operator">=</span> <span class="token constant">MAX</span>
        <span class="token keyword">var</span> begin <span class="token operator">=</span> <span class="token keyword">null</span>
        <span class="token keyword">var</span> end <span class="token operator">=</span> <span class="token keyword">null</span>

        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> distance<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> distance<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">var</span> tempBegin <span class="token operator">=</span> pointSet<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
                <span class="token keyword">var</span> tempEnd <span class="token operator">=</span> pointSet<span class="token punctuation">[</span>j<span class="token punctuation">]</span>

                <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">!=</span> j <span class="token comment">//去掉自己到自己的距离，因为都为0</span>
                    <span class="token operator">&amp;&amp;</span>
                    distance<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&lt;</span> minDis <span class="token comment">//小于目前的最短距离</span>
                    <span class="token operator">&amp;&amp;</span>
                    <span class="token function">canLink</span><span class="token punctuation">(</span>resultList<span class="token punctuation">,</span> tempBegin<span class="token punctuation">,</span> tempEnd<span class="token punctuation">)</span> <span class="token comment">//判断能否连接</span>
                <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    minDis <span class="token operator">=</span> distance<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span>
                    begin <span class="token operator">=</span> tempBegin
                    end <span class="token operator">=</span> tempEnd
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>minDis<span class="token punctuation">)</span>
        <span class="token function">link</span><span class="token punctuation">(</span>resultList<span class="token punctuation">,</span> begin<span class="token punctuation">,</span> end<span class="token punctuation">)</span>

        <span class="token comment">// 只存在一个部落，且这个部落里的村庄数和所有的村庄总和是相等的</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>resultList<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> resultList<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length <span class="token operator">==</span> pointSet<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">Kruskal</span><span class="token punctuation">(</span>pointSet<span class="token punctuation">,</span> distance<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>pointSet<span class="token punctuation">)</span>

输出：
<span class="token number">4</span> <span class="token comment">//AB</span>
<span class="token number">5</span> <span class="token comment">//CD</span>
<span class="token number">6</span> <span class="token comment">//BD</span>
<span class="token number">7</span> <span class="token comment">//DE</span>
<span class="token punctuation">[</span> Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'A'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'B'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'C'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'D'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'E'</span><span class="token punctuation">,</span> neighbor<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span>Node<span class="token punctuation">]</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>

</code></pre></div><hr> <h2 id="二叉搜索树"><a href="#二叉搜索树" class="header-anchor">#</a> 二叉搜索树</h2> <h6 id="问题"><a href="#问题" class="header-anchor">#</a> 问题：</h6> <p>有一万个数据，写一个方法，进行查找，查找给定的数，返回存在还是不存在，要求：尽可能的性能好。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">10000</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">0</span>

<span class="token keyword">function</span> <span class="token function">search</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        num <span class="token operator">+=</span> <span class="token number">1</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">search</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
</code></pre></div><p>二叉搜索树(二叉排序树)</p> <p>首先这是一颗二叉树</p> <p>其次有排序的效果，左子树的节点都比当前节点小，右子树的节点都比当前节点大</p> <p><img src="" alt="image"></p> <hr> <h2 id="构建二叉搜索树"><a href="#构建二叉搜索树" class="header-anchor">#</a> 构建二叉搜索树</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">10000</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token number">0</span>

<span class="token comment">// 传统的方法搜索</span>
<span class="token keyword">function</span> <span class="token function">search</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        num <span class="token operator">+=</span> <span class="token number">1</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">search</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span> <span class="token comment">//true 6488</span>

<span class="token comment">// 二叉树</span>
<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">addNode</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> num</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//num表示当前节点</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> num<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">// 目标值比当前节点大</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">&lt;</span> num<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 如果右侧为空，则创建节点</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>

        <span class="token comment">// 如果右侧有值，就形成递归</span>
        <span class="token keyword">else</span> <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> num<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 目标值比当前节点小</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>

        <span class="token keyword">else</span> <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> num<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">buildSearchTree</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">null</span>

    <span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>

<span class="token comment">// 二叉树搜索,类似前序遍历</span>

<span class="token keyword">var</span> num2 <span class="token operator">=</span> <span class="token number">0</span>

<span class="token keyword">function</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    num2 <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token function">buildSearchTree</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num2<span class="token punctuation">)</span> <span class="token comment">//true 16</span>
</code></pre></div><p><code>由测试得出，使用二叉树搜索树比传统方法性能好上近50倍以上，当我们希望二叉搜索树变为二叉平衡搜索树，性能优化</code></p> <hr> <h2 id="平衡二叉树"><a href="#平衡二叉树" class="header-anchor">#</a> 平衡二叉树</h2> <p><img src="/assets/img/20.e2de9bc8.png" alt="image"></p> <ol><li>根节点的左子树与右子树的高度差不能超过1</li> <li>这颗二叉树的每个子树都符合第一条规则</li></ol> <hr> <h2 id="用代码判断平衡二叉树"><a href="#用代码判断平衡二叉树" class="header-anchor">#</a> 用代码判断平衡二叉树</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'G'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> h <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'H'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> y <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'Y'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>left <span class="token operator">=</span> b
a<span class="token punctuation">.</span>right <span class="token operator">=</span> c
c<span class="token punctuation">.</span>left <span class="token operator">=</span> f
c<span class="token punctuation">.</span>right <span class="token operator">=</span> g
b<span class="token punctuation">.</span>left <span class="token operator">=</span> d
b<span class="token punctuation">.</span>right <span class="token operator">=</span> e
d<span class="token punctuation">.</span>right <span class="token operator">=</span> h
e<span class="token punctuation">.</span>right <span class="token operator">=</span> y

<span class="token keyword">function</span> <span class="token function">getDeep</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span>

    <span class="token comment">//选择左子树和右子树中的最大深度  + 1</span>
    <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token comment">// 判断平衡二叉树</span>
<span class="token keyword">function</span> <span class="token function">isBalance</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 根节点的左子树与右子树的高度差不能超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 这颗二叉树的每个子树都符合第一条规则</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre></div><hr> <h2 id="二叉树的单旋"><a href="#二叉树的单旋" class="header-anchor">#</a> 二叉树的单旋</h2> <p><code>把一个不平衡的二叉树变为平衡的二叉树，需要用到二叉树的单旋</code></p> <p><code>二叉树单旋操作(左单旋、右单旋)</code></p> <p>旋转节点：不平衡的节点为旋转节点</p> <p>新根：旋转之后称为根节点的节点</p> <p>变化分支：父级节点发生变化的那个分支</p> <p>不变分支：父级节点不变的那个分支</p> <p><code>1. 某个节点不平衡，如果左边浅，右边深，进行左单旋</code></p> <p><img src="/assets/img/21.47f2b028.png" alt="image"></p> <p><code>进行左单旋：</code></p> <ol><li>找到新根</li> <li>找到变化分支</li> <li>当前旋转节点的右孩子为变化分支</li> <li>新根的左孩子为旋转节点</li> <li>返回新的根节点</li></ol> <p><code>2. 某个节点不平衡，如果右边浅，左边深，进行右单旋</code></p> <p><img src="/assets/img/22.ecf1fd8e.png" alt="image"></p> <p><code>进行右单旋：</code></p> <ol><li>找到新根</li> <li>找到变化分支</li> <li>当前旋转节点的左孩子为变化分支</li> <li>新根的右孩子为旋转节点</li> <li>返回新的根节点</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'2'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'5'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'3'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node6 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'6'</span><span class="token punctuation">)</span>

<span class="token comment">// 左旋</span>
<span class="token comment">// node2.right = node5</span>
<span class="token comment">// node5.left = node3</span>
<span class="token comment">// node5.right = node6</span>

<span class="token comment">// 右旋</span>
node6<span class="token punctuation">.</span>left <span class="token operator">=</span> node3
node3<span class="token punctuation">.</span>left <span class="token operator">=</span> node2
node3<span class="token punctuation">.</span>right <span class="token operator">=</span> node5

<span class="token keyword">function</span> <span class="token function">getDeep</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span>

    <span class="token comment">//选择左子树和右子树中的最大深度 </span>
    <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token comment">// 判断平衡二叉树</span>
<span class="token keyword">function</span> <span class="token function">isBalance</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 根节点的左子树与右子树的高度差不能超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 这颗二叉树的每个子树都符合第一条规则</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// 左旋</span>
<span class="token keyword">function</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找到新根</span>
    <span class="token keyword">let</span> newRoot <span class="token operator">=</span> root<span class="token punctuation">.</span>right

    <span class="token comment">// 找到变化分支</span>
    <span class="token keyword">let</span> changeTree <span class="token operator">=</span> root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>left

    <span class="token comment">// 当前旋转节点的右孩子为变化分支</span>
    root<span class="token punctuation">.</span>right <span class="token operator">=</span> changeTree

    <span class="token comment">// 新根的左孩子为旋转节点</span>
    newRoot<span class="token punctuation">.</span>left <span class="token operator">=</span> root

    <span class="token comment">// 返回新的根节点</span>
    <span class="token keyword">return</span> newRoot
<span class="token punctuation">}</span>

<span class="token comment">// 右旋</span>
<span class="token keyword">function</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找到新根</span>
    <span class="token keyword">let</span> newRoot <span class="token operator">=</span> root<span class="token punctuation">.</span>left

    <span class="token comment">// 找到变化分支</span>
    <span class="token keyword">let</span> changeTree <span class="token operator">=</span> root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>right

    <span class="token comment">// 当前旋转节点的左孩子为变化分支</span>
    root<span class="token punctuation">.</span>left <span class="token operator">=</span> changeTree

    <span class="token comment">// 新根的右孩子为旋转节点</span>
    newRoot<span class="token punctuation">.</span>right <span class="token operator">=</span> root

    <span class="token comment">// 返回新的根节点</span>
    <span class="token keyword">return</span> newRoot
<span class="token punctuation">}</span>

<span class="token comment">// 把不平衡的二叉树变为平衡的二叉树，返回新的二叉树,按照后序遍历顺序进行操作</span>
<span class="token keyword">function</span> <span class="token function">change</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> root

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 不超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> root

    <span class="token comment">// 不平衡，左边深，进行右旋</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>leftDeep <span class="token operator">&gt;</span> rightDeep<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>

    <span class="token comment">// 不平衡，右边深，进行左旋</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
    
    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>


console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>node6<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// false</span>

<span class="token keyword">var</span> newRoot <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>node6<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span>
    <span class="token comment">// Node {</span>
    <span class="token comment">//     value: '3',</span>
    <span class="token comment">//     left: Node { value: '2', left: null, right: null },</span>
    <span class="token comment">//     right:</span>
    <span class="token comment">//      Node {</span>
    <span class="token comment">//        value: '6',</span>
    <span class="token comment">//        left: Node { value: '5', left: null, right: null },</span>
    <span class="token comment">//        right: null } }</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
</code></pre></div><hr> <h2 id="二叉树双旋的概念"><a href="#二叉树双旋的概念" class="header-anchor">#</a> 二叉树双旋的概念</h2> <p>二叉树的双旋(左右双旋、右左双旋)</p> <p><code>变化分支，不可以是唯一的最深分支，如果变化分支是唯一的最深分支，要先进行反向旋转</code></p> <p>当要对某个节点进行左单旋时，
如果变化分支是唯一的最深分支，那么我们要对新根进行右单旋，然后再进行左单旋
这样的旋转叫做<code>右左双旋</code></p> <p><img src="/assets/img/23.633a0418.png" alt="image"></p> <p>当要对某个节点进行右单旋时，
如果变化分支是唯一的最深分支，那么我们要对新根进行左单旋，然后再进行右单旋
这样的旋转叫做<code>左右双旋</code></p> <p><img src="/assets/img/24.bfe26031.png" alt="image"></p> <hr> <h2 id="二叉树双旋的代码实现"><a href="#二叉树双旋的代码实现" class="header-anchor">#</a> 二叉树双旋的代码实现</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> node8 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'8'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node7 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'7'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node6 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'6'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'5'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'2'</span><span class="token punctuation">)</span>

<span class="token comment">// 左右双旋</span>
<span class="token comment">// node8.left = node7</span>
<span class="token comment">// node7.left = node6</span>
<span class="token comment">// node6.left = node5</span>
<span class="token comment">// node5.left = node2</span>

<span class="token comment">// 右左双旋</span>
node2<span class="token punctuation">.</span>right <span class="token operator">=</span> node5
node5<span class="token punctuation">.</span>right <span class="token operator">=</span> node6
node6<span class="token punctuation">.</span>right <span class="token operator">=</span> node7
node7<span class="token punctuation">.</span>right <span class="token operator">=</span> node8

<span class="token keyword">function</span> <span class="token function">getDeep</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span>

    <span class="token comment">//选择左子树和右子树中的最大深度 </span>
    <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token comment">// 判断平衡二叉树</span>
<span class="token keyword">function</span> <span class="token function">isBalance</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 根节点的左子树与右子树的高度差不能超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 这颗二叉树的每个子树都符合第一条规则</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// 左旋</span>
<span class="token keyword">function</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找到新根</span>
    <span class="token keyword">let</span> newRoot <span class="token operator">=</span> root<span class="token punctuation">.</span>right

    <span class="token comment">// 找到变化分支</span>
    <span class="token keyword">let</span> changeTree <span class="token operator">=</span> root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>left

    <span class="token comment">// 当前旋转节点的右孩子为变化分支</span>
    root<span class="token punctuation">.</span>right <span class="token operator">=</span> changeTree

    <span class="token comment">// 新根的左孩子为旋转节点</span>
    newRoot<span class="token punctuation">.</span>left <span class="token operator">=</span> root

    <span class="token comment">// 返回新的根节点</span>
    <span class="token keyword">return</span> newRoot
<span class="token punctuation">}</span>

<span class="token comment">// 右旋</span>
<span class="token keyword">function</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找到新根</span>
    <span class="token keyword">let</span> newRoot <span class="token operator">=</span> root<span class="token punctuation">.</span>left

    <span class="token comment">// 找到变化分支</span>
    <span class="token keyword">let</span> changeTree <span class="token operator">=</span> root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>right

    <span class="token comment">// 当前旋转节点的左孩子为变化分支</span>
    root<span class="token punctuation">.</span>left <span class="token operator">=</span> changeTree

    <span class="token comment">// 新根的右孩子为旋转节点</span>
    newRoot<span class="token punctuation">.</span>right <span class="token operator">=</span> root

    <span class="token comment">// 返回新的根节点</span>
    <span class="token keyword">return</span> newRoot
<span class="token punctuation">}</span>

<span class="token comment">// 把不平衡的二叉树变为平衡的二叉树，返回新的二叉树,按照后序遍历顺序进行操作</span>
<span class="token keyword">function</span> <span class="token function">change</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> root

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 不超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> root
    <span class="token punctuation">}</span>

    <span class="token comment">// 不平衡，左边深，进行右旋</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>leftDeep <span class="token operator">&gt;</span> rightDeep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        
        <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>左右双旋<span class="token operator">--</span><span class="token operator">&gt;</span>
        <span class="token keyword">let</span> changeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
        <span class="token keyword">let</span> nochangeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>changeTreeDeep <span class="token operator">&gt;</span> nochangeTreeDeep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>


    <span class="token comment">// 不平衡，右边深，进行左旋</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>右左双旋<span class="token operator">--</span><span class="token operator">&gt;</span>
        <span class="token keyword">let</span> changeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
        <span class="token keyword">let</span> nochangeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>changeTreeDeep <span class="token operator">&gt;</span> nochangeTreeDeep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>


console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>node2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// false</span>

<span class="token keyword">var</span> newRoot <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>node2<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span>
<span class="token comment">// Node {</span>
<span class="token comment">//     value: '5',</span>
<span class="token comment">//     left: Node { value: '2', left: null, right: null },</span>
<span class="token comment">//     right:</span>
<span class="token comment">//      Node {</span>
<span class="token comment">//        value: '7',</span>
<span class="token comment">//        left: Node { value: '6', left: null, right: null },</span>
<span class="token comment">//        right: Node { value: '8', left: null, right: null } } }</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true  数据量大就不平衡</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>以下数据量大会发生二叉树不平衡<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token keyword">function</span> <span class="token function">addNode</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> num</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//num表示当前节点</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> num<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">// 目标值比当前节点大</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">&lt;</span> num<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 如果右侧为空，则创建节点</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>

        <span class="token comment">// 如果右侧有值，就形成递归</span>
        <span class="token keyword">else</span> <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> num<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 目标值比当前节点小</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>

        <span class="token keyword">else</span> <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> num<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">buildSearchTree</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">null</span>

    <span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>

<span class="token keyword">var</span> num2 <span class="token operator">=</span> <span class="token number">0</span>

<span class="token keyword">function</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    num2 <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token function">buildSearchTree</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num2<span class="token punctuation">)</span> <span class="token comment">// true 16</span>

<span class="token keyword">var</span> newRoot <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
num2 <span class="token operator">=</span> <span class="token number">0</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">searchByTree</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num2<span class="token punctuation">)</span> <span class="token comment">// true 13</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//false 因为不平衡我们没有考虑左左双旋、右右双旋</span>
</code></pre></div><hr> <h2 id="左左双旋与右右双旋"><a href="#左左双旋与右右双旋" class="header-anchor">#</a> 左左双旋与右右双旋</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> node8 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'8'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node7 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'7'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node6 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'6'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'5'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> node2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'2'</span><span class="token punctuation">)</span>

<span class="token comment">// 左右双旋</span>
<span class="token comment">// node8.left = node7</span>
<span class="token comment">// node7.left = node6</span>
<span class="token comment">// node6.left = node5</span>
<span class="token comment">// node5.left = node2</span>

<span class="token comment">// 右左双旋</span>
node2<span class="token punctuation">.</span>right <span class="token operator">=</span> node5
node5<span class="token punctuation">.</span>right <span class="token operator">=</span> node6
node6<span class="token punctuation">.</span>right <span class="token operator">=</span> node7
node7<span class="token punctuation">.</span>right <span class="token operator">=</span> node8

<span class="token keyword">function</span> <span class="token function">getDeep</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span>

    <span class="token comment">//选择左子树和右子树中的最大深度 </span>
    <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
<span class="token punctuation">}</span>

<span class="token comment">// 判断平衡二叉树</span>
<span class="token keyword">function</span> <span class="token function">isBalance</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 根节点的左子树与右子树的高度差不能超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token comment">// 这颗二叉树的每个子树都符合第一条规则</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// 左旋</span>
<span class="token keyword">function</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找到新根</span>
    <span class="token keyword">let</span> newRoot <span class="token operator">=</span> root<span class="token punctuation">.</span>right

    <span class="token comment">// 找到变化分支</span>
    <span class="token keyword">let</span> changeTree <span class="token operator">=</span> root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>left

    <span class="token comment">// 当前旋转节点的右孩子为变化分支</span>
    root<span class="token punctuation">.</span>right <span class="token operator">=</span> changeTree

    <span class="token comment">// 新根的左孩子为旋转节点</span>
    newRoot<span class="token punctuation">.</span>left <span class="token operator">=</span> root

    <span class="token comment">// 返回新的根节点</span>
    <span class="token keyword">return</span> newRoot
<span class="token punctuation">}</span>

<span class="token comment">// 右旋</span>
<span class="token keyword">function</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 找到新根</span>
    <span class="token keyword">let</span> newRoot <span class="token operator">=</span> root<span class="token punctuation">.</span>left

    <span class="token comment">// 找到变化分支</span>
    <span class="token keyword">let</span> changeTree <span class="token operator">=</span> root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>right

    <span class="token comment">// 当前旋转节点的左孩子为变化分支</span>
    root<span class="token punctuation">.</span>left <span class="token operator">=</span> changeTree

    <span class="token comment">// 新根的右孩子为旋转节点</span>
    newRoot<span class="token punctuation">.</span>right <span class="token operator">=</span> root

    <span class="token comment">// 返回新的根节点</span>
    <span class="token keyword">return</span> newRoot
<span class="token punctuation">}</span>

<span class="token comment">// 把不平衡的二叉树变为平衡的二叉树，返回新的二叉树,按照后序遍历顺序进行操作</span>
<span class="token keyword">function</span> <span class="token function">change</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> root

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token keyword">var</span> leftDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
    <span class="token keyword">var</span> rightDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

    <span class="token comment">// 不超过1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>leftDeep <span class="token operator">-</span> rightDeep<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> root
    <span class="token punctuation">}</span>

    <span class="token comment">// 不平衡，左边深，进行右旋</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>leftDeep <span class="token operator">&gt;</span> rightDeep<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 左右双旋</span>
        <span class="token keyword">let</span> changeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
        <span class="token keyword">let</span> nochangeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">.</span>left<span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>changeTreeDeep <span class="token operator">&gt;</span> nochangeTreeDeep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 右右双旋</span>
        <span class="token keyword">let</span> newRoot <span class="token operator">=</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
        newRoot<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
        newRoot <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span>
        <span class="token keyword">return</span> newRoot
    <span class="token punctuation">}</span>


    <span class="token comment">// 不平衡，右边深，进行左旋</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 右左双旋</span>
        <span class="token keyword">let</span> changeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
        <span class="token keyword">let</span> nochangeTreeDeep <span class="token operator">=</span> <span class="token function">getDeep</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">.</span>right<span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>changeTreeDeep <span class="token operator">&gt;</span> nochangeTreeDeep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token function">rightRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 左左双旋</span>
        <span class="token keyword">let</span> newRoot <span class="token operator">=</span> <span class="token function">leftRotate</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
        newRoot<span class="token punctuation">,</span> left <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">.</span>left<span class="token punctuation">)</span>
        newRoot <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span>
        <span class="token keyword">return</span> newRoot
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>

</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>
<span class="token keyword">function</span> <span class="token function">addNode</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> num</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//num表示当前节点</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> num<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>

    <span class="token comment">// 目标值比当前节点大</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">&lt;</span> num<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 如果右侧为空，则创建节点</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>right <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>

        <span class="token comment">// 如果右侧有值，就形成递归</span>
        <span class="token keyword">else</span> <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> num<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 目标值比当前节点小</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> root<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>

        <span class="token keyword">else</span> <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> num<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">buildSearchTree</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">null</span>

    <span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">addNode</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> root
<span class="token punctuation">}</span>

<span class="token keyword">var</span> num2 <span class="token operator">=</span> <span class="token number">0</span>

<span class="token keyword">function</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    num2 <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">&gt;</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> root <span class="token operator">=</span> <span class="token function">buildSearchTree</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">searchByTree</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num2<span class="token punctuation">)</span> <span class="token comment">// false 23</span>

<span class="token keyword">var</span> newRoot <span class="token operator">=</span> <span class="token function">change</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span>
num2 <span class="token operator">=</span> <span class="token number">0</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">searchByTree</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> num2<span class="token punctuation">)</span> <span class="token comment">// false 11</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isBalance</span><span class="token punctuation">(</span>newRoot<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true 数据量大也平衡</span>
</code></pre></div><hr> <h2 id="_234树的由来"><a href="#_234树的由来" class="header-anchor">#</a> 234树的由来</h2> <p>二叉平衡排序树是极致的吗？ 答： 不是</p> <p>如果我们要提升二叉平衡排序树的性能该如何做？</p> <p>影响二叉平衡排序树的性能的点在哪？
答：<code>在于二叉平衡排序树只能有两个叉，导致在节点铺满的时候也会有很多层。希望可以一个节点存多个数。可以提升空间的性能</code></p> <p>如何才能让查找的效率尽可能的少？答： <code>树的层级越少，查找效率越高</code></p> <p>怎么样才能让二叉平衡排序树的层数变的更少？
答：<code>如果不是二叉，层数会更少</code></p> <p><code>叉越多，层数越少，但是叉越多，树的结构就越复杂，一般我们认为有4个叉的时候性能最好，就出现234树的由来</code></p> <p><code>234树：</code></p> <p><img src="" alt="image"></p> <p><code>我们希望有一颗树，最多有四个叉(度为4)</code></p> <ol><li>234树子节点永远在最后一层</li> <li>234树<code>永远是平衡</code>的(每一个路径高度都相同)</li></ol> <p>达到了一定的效果
分支变多了，层数变少了
节点中存的树变多了，节点变少了</p> <p>因为分支多了，所以复杂度上升了</p> <p>希望对234树进行简化</p> <p>希望能简化为二叉树
希望依旧保留多叉
希望依旧单节点中存放多个值</p> <hr> <h4 id="红黑树"><a href="#红黑树" class="header-anchor">#</a> 红黑树</h4> <ol><li>节点是红色或黑色</li> <li>根节点是黑色</li> <li>每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径不能有两个连续的红色节点)</li> <li>从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点</li></ol> <p>未完待续！！！</p> <hr> <h2 id="树的深度优先搜索"><a href="#树的深度优先搜索" class="header-anchor">#</a> 树的深度优先搜索</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 树的深度优先搜索</span>

<span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>childs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
a<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
a<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">deepSearch</span><span class="token punctuation">(</span><span class="token parameter">root<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>
    <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> root<span class="token punctuation">.</span>childs<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        result <span class="token operator">|=</span> <span class="token function">deepSearch</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>childs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> target<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> result <span class="token operator">?</span> <span class="token boolean">true</span> <span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">deepSearch</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">deepSearch</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">'n'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//false</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>树的深度遍历<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token keyword">function</span> <span class="token function">deepSearchBianli</span><span class="token punctuation">(</span><span class="token parameter">root</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> root<span class="token punctuation">.</span>childs<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">deepSearchBianli</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>childs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">deepSearchBianli</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">// A B C E F D 深搜</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
输出：
Node <span class="token punctuation">{</span>
  value<span class="token operator">:</span> <span class="token string">'A'</span><span class="token punctuation">,</span>
  childs<span class="token operator">:</span>
   <span class="token punctuation">[</span> Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'B'</span><span class="token punctuation">,</span> childs<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
     Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'C'</span><span class="token punctuation">,</span> childs<span class="token operator">:</span> <span class="token punctuation">[</span>Array<span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
     Node <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token string">'D'</span><span class="token punctuation">,</span> childs<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span>
</code></pre></div><hr> <h2 id="树的广度优先搜索"><a href="#树的广度优先搜索" class="header-anchor">#</a> 树的广度优先搜索</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>childs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'F'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
a<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
a<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>childs<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">bfc</span><span class="token punctuation">(</span><span class="token parameter">roots<span class="token punctuation">,</span> target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>roots <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> roots<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">var</span> childs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> roots<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>roots<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">true</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            childs <span class="token operator">=</span> childs<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>roots<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>childs<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">bfc</span><span class="token punctuation">(</span>childs<span class="token punctuation">,</span> target<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">bfc</span><span class="token punctuation">(</span><span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">bfc</span><span class="token punctuation">(</span><span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'n'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//false</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 实现广度优先遍历</span>
<span class="token keyword">function</span> <span class="token function">bfcBianli</span><span class="token punctuation">(</span><span class="token parameter">roots</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>roots <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>roots<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token keyword">var</span> childs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> roots<span class="token punctuation">.</span>childs<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>roots<span class="token punctuation">.</span>childs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span>
        childs <span class="token operator">=</span> childs<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>roots<span class="token punctuation">.</span>childs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>childs<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> childs<span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>childs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
<span class="token function">bfcBianli</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token comment">// A B C D E F </span>
</code></pre></div><hr> <h2 id="图的深度优先搜索"><a href="#图的深度优先搜索" class="header-anchor">#</a> 图的深度优先搜索</h2> <p><img src="/assets/img/26.8ae8e20c.png" alt="image"></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>neighbor <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
a<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>

b<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
b<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
b<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>

c<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>

d<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
d<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
d<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>

e<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>

<span class="token comment">// 图的深度优先搜索</span>
<span class="token keyword">function</span> <span class="token function">deepSearch</span><span class="token punctuation">(</span><span class="token parameter">node<span class="token punctuation">,</span> target<span class="token punctuation">,</span> path</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>node <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>
    
    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>比较新插入的点是否比较过<span class="token operator">--</span><span class="token operator">&gt;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>path<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>node<span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>

    path<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span>

    <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token boolean">false</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> node<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        result <span class="token operator">|=</span> <span class="token function">deepSearch</span><span class="token punctuation">(</span>node<span class="token punctuation">.</span>neighbor<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> target<span class="token punctuation">,</span> path<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> result <span class="token operator">?</span> <span class="token boolean">true</span> <span class="token operator">:</span> <span class="token boolean">false</span>

<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">deepSearch</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">deepSearch</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">'n'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//false</span>
</code></pre></div><hr> <h2 id="图的广度优先搜索"><a href="#图的广度优先搜索" class="header-anchor">#</a> 图的广度优先搜索</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">Node</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> value
    <span class="token keyword">this</span><span class="token punctuation">.</span>neighbor <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'D'</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> e <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token string">'E'</span><span class="token punctuation">)</span>

a<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
a<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>

b<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
b<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
b<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>

c<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
c<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>

d<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
d<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
d<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>

e<span class="token punctuation">.</span>neighbor<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span>

<span class="token comment">// 图的广度优先搜索</span>
<span class="token keyword">function</span> <span class="token function">bfs</span><span class="token punctuation">(</span><span class="token parameter">nodes<span class="token punctuation">,</span> target<span class="token punctuation">,</span> path</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>nodes <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> nodes<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span>

    <span class="token keyword">var</span> nextNodes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nodes<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    
        <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>比较新插入的点是否比较过<span class="token operator">--</span><span class="token operator">&gt;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>path<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>nodes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>

        path<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>nodes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>nodes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>value <span class="token operator">==</span> target<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>
        <span class="token keyword">else</span> nextNodes <span class="token operator">=</span> nextNodes<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>nodes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>neighbor<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">bfs</span><span class="token punctuation">(</span>nextNodes<span class="token punctuation">,</span> target<span class="token punctuation">,</span> path<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">bfs</span><span class="token punctuation">(</span><span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">bfs</span><span class="token punctuation">(</span><span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'n'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//false</span>
</code></pre></div><hr> <h2 id="动态规划之斐波那契数列"><a href="#动态规划之斐波那契数列" class="header-anchor">#</a> 动态规划之斐波那契数列</h2> <p>递归是通过减少问题规模来求子问题的解，得到最小子问题的解后，主问题就能得到答案；动态规划则是一种递推算法，解决最小子问题然后逐一合并，最终变成主问题的解</p> <p>实现一个计算斐波那契数列的方法: 1,1,2,3,5,8,13...</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 动态规划，笔试遇到动态规划是后面的大题</span>

<span class="token comment">// 0 ， 1 ， 1 ， 2 ， 3 ， 5 ， 8 ， 13...</span>
<span class="token comment">// 给出第n 位，问第n位值是多少？</span>

<span class="token keyword">function</span> <span class="token function">fibo1</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">1</span>

    <span class="token keyword">let</span> first <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>
        seconds <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
        third<span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        third <span class="token operator">=</span> first <span class="token operator">+</span> seconds
        first <span class="token operator">=</span> seconds
        seconds <span class="token operator">=</span> third
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> third
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">fibo1</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//13</span>

<span class="token comment">// f(n) = f(n - 1) + f(n -2)</span>
<span class="token keyword">function</span> <span class="token function">fibo2</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">1</span>

    <span class="token keyword">return</span> <span class="token function">fibo2</span><span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">fibo2</span><span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">fibo2</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//13</span>
</code></pre></div><hr> <h2 id="动态规划之青蛙跳台阶问题"><a href="#动态规划之青蛙跳台阶问题" class="header-anchor">#</a> 动态规划之青蛙跳台阶问题</h2> <p>一个青蛙，一次只能跳一级台阶，或者跳两级台阶，问，这个青蛙跳上n级台阶有多少种跳法？</p> <p><code>分析：</code></p> <p>如果这只青蛙，跳上了第n级台阶，那么最后一次跳跃之前，他一定在n - 1级台阶或者 n - 2级台阶上</p> <p>那么跳上n级台阶有多少种情况就变成了两个子问题</p> <p>跳上n - 1级台阶的跳法 加上 跳上n - 级台阶的跳法</p> <p>按照此逻辑递推，跳上n - 1级台阶可以拆解为两个子问题 即：跳上n - 2级台阶的跳法 加上 跳上n - 3级台阶的跳法</p> <p>.
.
.
即： f(n) = f(n - 1) + f(n - 2)</p> <div class="language-js extra-class"><div class="highlight-lines"><br><br><br><div class="highlighted"> </div><br><br><br><br><br><br><br><br><br><br><br><br><br></div><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">jump</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">2</span>
    <span class="token keyword">return</span> <span class="token function">jump</span><span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">jump</span><span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">jump</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//8</span>

<span class="token comment">// 1 2 2</span>
<span class="token comment">// 2 2 1</span>
<span class="token comment">// 2 1 2</span>
<span class="token comment">// 1 1 1 1 1</span>
<span class="token comment">// 2 1 1 1</span>
<span class="token comment">// 1 2 1 1</span>
<span class="token comment">// 1 1 2 1</span>
<span class="token comment">// 1 1 1 2 有8种跳法</span>
</code></pre></div><hr> <h2 id="动态规划之变态青蛙跳台阶问题"><a href="#动态规划之变态青蛙跳台阶问题" class="header-anchor">#</a> 动态规划之变态青蛙跳台阶问题</h2> <p>一只青蛙，一次可以跳1级台阶，2级台阶、或n级台阶。问： 这只青蛙，跳上n级台阶有多少种方法？</p> <p><code>分析：</code></p> <p>f(n) = f( n- 1) + f(n -2) + f(n -3) +......+f(2) + f(1) + f(0)</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">jump</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span>

    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        result <span class="token operator">+=</span> <span class="token function">jump</span><span class="token punctuation">(</span>n <span class="token operator">-</span> i<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> result <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">//加上1代表从0级台阶直接跳上去的场景</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">jump</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//16</span>

<span class="token comment">//  1 1 1 1 1 </span>
<span class="token comment">//  1 1 1 2</span>
<span class="token comment">// 1  1 2 1</span>
<span class="token comment">// 1 2 1 1</span>
<span class="token comment">// 2 1 1 1</span>
<span class="token comment">// 1 2 2</span>
<span class="token comment">// 2 1 2</span>
<span class="token comment">// 2 2 1</span>
<span class="token comment">// 1 1 3 </span>
<span class="token comment">// 3 1 1</span>
<span class="token comment">// 1 3 1</span>
<span class="token comment">// 2 3 </span>
<span class="token comment">// 3 2</span>
<span class="token comment">// 1 4</span>
<span class="token comment">// 4 1</span>
<span class="token comment">// 5</span>
<span class="token comment">// 有16种跳法</span>
</code></pre></div></div> <footer class="page-edit" style="display:none;"><div class="edit-link"><a href="https://github.com/zscnb/blog/edit/master/docs/views/blog/algorithm.md" target="_blank" rel="noopener noreferrer">HELP ME</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time"> 2021/05/29 15:49:38</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><!----><div class="copydialog" style="display:none;" data-v-31384c7b data-v-31384c7b><div class="dislog-mask" data-v-31384c7b></div> <div class="copy-limit-dialog" data-v-31384c7b><div class="close-dialog-btn" data-v-31384c7b></div> <div class="main-content" data-v-31384c7b><span class="tip-message" data-v-31384c7b>
          文档复制为VIP权益，开通VIP可继续复制
        </span> <ul class="list" data-v-31384c7b><li class="xztq-r" data-v-31384c7b>享全站VIP文档下载特权</li> <li class="mfzx-r" data-v-31384c7b>与博主亲密交流特权</li> <li class="eight-count-r" data-v-31384c7b>
            随时随地随心下载
          </li> <li class="gszh-r" data-v-31384c7b>文档多格式转化</li></ul> <span class="buy-vip-btn" data-v-31384c7b><a href="https://ftp.bmp.ovh/imgs/2021/05/dd79da2e8a65b1d5.jpg" target="_blank" rel="noopener noreferrer" data-v-31384c7b>开通VIP, 享受无限制复制特权</a></span></div></div></div><div class="box" style="display:none;" data-v-bb7a82ec>
  春节快乐
  <div class="light" data-v-bb7a82ec><div class="line" data-v-bb7a82ec></div> <div class="light-a" data-v-bb7a82ec><div class="light-b" data-v-bb7a82ec></div> <div class="light-c" data-v-bb7a82ec>
        新
      </div> <div class="ear-a" data-v-bb7a82ec><div class="ear-b" data-v-bb7a82ec></div> <div class="ear-c" data-v-bb7a82ec></div></div></div></div></div><div data-v-50ef6c52><div class="DetailsOpenFlag" style="right:1rem;bottom:9rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;font-size:14px;font-weight:500;display:none;" data-v-50ef6c52>
 展开 

</div></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 青灯有味
    </div> <div class="operation" style="right:56px;bottom:42px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="right:56px;bottom:-8px;opacity:1;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><div class="reco-bgm-panel" data-v-39f9e6e0><audio id="bgm" src="http://zscnb.gitee.io/upload/static/audio/五里 - 乌兰巴托的夜.mp3" data-v-39f9e6e0></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><img src="http://zscnb.gitee.io/upload/static/img/wulan.png" data-v-39f9e6e0></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><div class="reco-bgm-cover" style="background-image:url(http://zscnb.gitee.io/upload/static/img/wulan.png);" data-v-39f9e6e0><div class="mini-operation" style="display:none;" data-v-39f9e6e0><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-39f9e6e0></i></div> <div class="falut-message" style="display:none;" data-v-39f9e6e0>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><div class="info-box" data-v-39f9e6e0><i class="reco-bgm reco-bgm-music music" data-v-39f9e6e0></i>五里-乌兰巴托的夜</div> <div class="info-box" data-v-39f9e6e0><i class="reco-bgm reco-bgm-artist" data-v-39f9e6e0></i>乌兰巴托的夜</div> <div class="reco-bgm-progress" data-v-39f9e6e0><div class="progress-bar" data-v-39f9e6e0><div class="bar" data-v-39f9e6e0></div></div></div> <div class="reco-bgm-operation" data-v-39f9e6e0><i class="reco-bgm reco-bgm-last last" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-play play" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-next next" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-39f9e6e0></i> <div class="volume-bar" data-v-39f9e6e0><div class="bar" data-v-39f9e6e0></div></div></div></div> <div class="reco-bgm-left-box" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><i class="reco-bgm reco-bgm-left" data-v-39f9e6e0></i></div></div></div><div></div><div id="musicPlayer" data-v-b7b63636><!----> <div class="bbox" data-v-b7b63636><div class="pan" style="background-image:url(/assets/img/pan.07613e22.png);" data-v-b7b63636><img src="" alt class="pan_c" data-v-b7b63636></div> <div class="box" style="background-image:url();" data-v-b7b63636><div class="music_shlter_2" style="background-image:url();" data-v-b7b63636></div> <div class="music_shlter" style="background-image:url();" data-v-b7b63636></div> <div class="music_shlter_3" data-v-b7b63636></div> <div class="music_dis" data-v-b7b63636><div class="dis_list" data-v-b7b63636>···</div> <p class="music_title" data-v-b7b63636></p> <p class="music_intro" data-v-b7b63636>歌手: </p> <ul class="music_words" data-v-b7b63636><div class="music_words_box" style="top:0px;" data-v-b7b63636></div></ul></div> <div class="control_box" data-v-b7b63636><div class="control_button" data-v-b7b63636><img src="" alt class="control_icon" data-v-b7b63636></div> <div class="progress" data-v-b7b63636><div class="progress_c" style="width:0%;" data-v-b7b63636><div class="progress_circle" data-v-b7b63636><div class="progress_circle_c" data-v-b7b63636></div></div></div></div></div></div> <video id="music" autoplay="autoplay" src="" name="media" data-v-b7b63636></video></div></div></div></div>
    <script src="/assets/js/app.3af45ec9.js" defer></script><script src="/assets/js/6.5ffa3a3c.js" defer></script><script src="/assets/js/1.60e80d20.js" defer></script><script src="/assets/js/7.cb2bf59a.js" defer></script><script src="/assets/js/26.ba28b685.js" defer></script><script src="/assets/js/28.9dabefed.js" defer></script><script src="/assets/js/36.10d521da.js" defer></script>
  </body>
</html>
